PDA

View Full Version : [Java] help with KeyStroke class


punx
10-07-2005, 07:32 PM
What do you need to import to instantiate a newy KeyStroke object? Heres the code I have:

import java.awt.AWTKeyStroke;
import javax.swing.KeyStroke;
...
KeyStroke k = new KeyStroke();
...
System.out.println("Type something: ");
key = k.getKeyStroke();
System.out.println("The key you pressed was: "+key+".\n");
...

When I compile the program I get.

--------------------Configuration: <Default>--------------------
C:\Documents and Settings\admin\Desktop\KeyLogger.java:19: cannot resolve symbol
symbol : constructor KeyStroke ()
location: class javax.swing.KeyStroke
KeyStroke k = new KeyStroke();
^
C:\Documents and Settings\admin\Desktop\KeyLogger.java:26: cannot resolve symbol
symbol : method getKeyStroke ()
location: class javax.swing.KeyStroke
key = k.getKeyStroke();

Any tips? Thanks.

suryad
10-11-2005, 09:23 AM
If you look at the API documentation you will notice that there is no constructors for this class meaning that this is a static class, a class where you do not need to use the new operator. Meaning you dont need to instantiate the class into an object to be able to use its methods. Notice in the documentation the modifier keyword in front of the methods labeled static. So all you have to do is say KeyStroke.nameOfRelevantMethod().

Hope that helps.

punx
10-11-2005, 05:51 PM
Ill look into it. Thanks.