2

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 ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
LuSyo
  • 35
  • 1
  • 3

1 Answers1

4

2 comments:

If you change your statement to System.out.println((int)ks.getKeyChar()); you will see that keyChar is 65535, which is the value of KeyEvent.CHAR_UNDEFINED.

You could try AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke("typed A"); to see that keyChar is A and you could also try AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke("typed F4"); to see that you get an exception.

assylias
  • 321,522
  • 82
  • 660
  • 783