質問

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 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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top