Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyCode for the Key ( ? ß \ )

Tags:

java

keycode

I am using a German Keyboard (shown below) and trying the Robot Class in Java. I am trying to find the KeyCodes for the keys I pressed. It works with getKeyCode(). For example: 'A' is Code: 65, '-' is Code: 45, 'ENTER' is Code: 10

But when I press the '? ß \' key (on German Keyboards right of the 1-0 keys above) getKeyCode() says Code: 0 and I didn't find any VK_KEYin the documentary either.

Is there any way to press that key?


German keyboard

like image 738
DroiDar Avatar asked Dec 28 '14 21:12

DroiDar


4 Answers

Backslash \ is apparently considered to be the primary character of this key. So KeyEvent.VK_BACK_SLASH gives you the key-code of that key on a German keyboard.

like image 102
Olaf Naumann Avatar answered Nov 18 '22 22:11

Olaf Naumann


The key codes are for keyboards with English / US layouts. Try VK_EQUALS for the key itself but Robot might actually send a = instead.

If you don't need the actual key to be pressed but the character entered, you can try to simulate Unicode input via Alt+Unicode code point. See this question: How to make the Java.awt.Robot type unicode characters? (Is it possible?)

See also this answer: https://stackoverflow.com/a/14766664/34088 It points to a library which uses keyboard layouts to map Java characters to keys. RoboticAutomaton.typeCharacter() uses the keyboard layout to find out how which keys to press to get a certain character.

like image 41
Aaron Digulla Avatar answered Nov 18 '22 22:11

Aaron Digulla


Well java supports around 44000 different characters including the ASCII characters so do expect some new things also if you want to see which key you are pressing and which one is pressed just print them out in the keyPressed method by getExtendedKeyCode() if its not a standard key and also print out the key it self.

like image 1
Tayyab Kazmi Avatar answered Nov 18 '22 21:11

Tayyab Kazmi


The javadoc of KeyEvent says:

Not all characters have a keycode associated with them. For example, there is no keycode for the question mark because there is no keyboard for which it appears on the primary layer.

The ß character is such a character. However, all key press events have a consistent extended key code that can be found using the utility method KeyEvent.getExtendedKeyCodeForChar() and compared to the one from the key event:

if (keyEvent.getExtendedKeyCode() == KeyEvent.getExtendedKeyCodeForChar('ß')) {
    // ß was pressed
}
like image 1
Bohemian Avatar answered Nov 18 '22 22:11

Bohemian