I have to work with a string standing for a keystroke (for instance “A”, “ENTER”, “F4”). From this string I need to get the keychar, the keycode and the key modifiers for the keystroke.
Here is what I do (for F4 for example) :
AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke("F4");
System.out.println(ks.getKeyCode());
System.out.println(ks.getKeyModifiers());
System.out.println(ks.getKeyChar());
I get :
115
0
?
keycode and modifiers are OK but whatever keystroke I try I always get ? for the keychar …
Am I missing something ?
2 comments:
If you change your statement to
System.out.println((int)ks.getKeyChar());you will see thatkeyCharis 65535, which is the value ofKeyEvent.CHAR_UNDEFINED.You could try
AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke("typed A");to see that keyChar isAand you could also tryAWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke("typed F4");to see that you get an exception.