12

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

8
  • 1
    Have you tried using the getExtendedKeyCode() instead? Dec 28, 2014 at 21:53
  • Are you pressing ß or some combination? What do you mean ? ß \ ?
    – Bohemian
    Dec 28, 2014 at 21:54
  • @Bohemian Normal Keypress on that Key gives a 'ß' with Shift and that Key it's a '?' and with Alt Gr and that Key its a '\'
    – DroiDar
    Dec 28, 2014 at 22:02
  • @AnthonyForloney with the getExtendedKeyCode() it gives me the Number 16777439 but it says InvalidKeyCode
    – DroiDar
    Dec 28, 2014 at 22:05

4 Answers 4

2

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.

1
  • This is actually the one working for me, thank you :). The getExtendedKeyCodeForChar method just gives me a keycode which the robot does not recognized.
    – OhDaeSu
    Mar 20, 2019 at 20:21
1

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.

2
  • > The key codes are for keyboards with English / US layouts. Then why are there plenty of foreign keycodes in KeyEvent, e.g. VK_JAPANESE_HIRAGANA just to name one.
    – stewori
    Jul 25, 2019 at 21:31
  • @stewori That's not a key like "a" but something like Shift or Ctrl and switches between input methods (Hiragana vs. Katakana). That said, there are actual "letter" keys for many foreign characters (like XK_kana_*) but they probably won't work in Robot since the app will try to convert the key code into a string using the current keyboard layout and that won't contain the code. Aug 12, 2019 at 14:09
1

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
}
2
0

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.

2
  • System.out.println("Key: " + e.getKeyChar() + ", Code: " + e.getKeyCode() + " Extended: " +e.getExtendedKeyCode()); gives me the KeyCodes, but when i try it with KeyPress and KeyRelease The Exception Says: Invalid Key Code
    – DroiDar
    Dec 29, 2014 at 20:33
  • Could you elaborate a bit Dec 29, 2014 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.