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_KEY
in the documentary either.
Is there any way to press that key?
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With