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

jww
  • 97,681
  • 90
  • 411
  • 885
DroiDar
  • 115
  • 1
  • 6
  • 1
    Have you tried using the `getExtendedKeyCode()` instead? – Anthony Forloney Dec 28 '14 at 21:53
  • Are you pressing `ß` or some combination? What do you mean `? ß \ `? – Bohemian Dec 28 '14 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 '14 at 22:02
  • @AnthonyForloney with the `getExtendedKeyCode()` it gives me the Number 16777439 but it says InvalidKeyCode – DroiDar Dec 28 '14 at 22:05
  • http://stackoverflow.com/questions/397113/how-to-make-the-java-awt-robot-type-unicode-characters-is-it-possible – lbalazscs Dec 28 '14 at 22:48
  • @droi I have a theory that might work, but I need more data. Can you tell me what the extended codes are for the umlaut letters? – Bohemian Dec 29 '14 at 01:31
  • @Bohemian Do you mean that?
      Key: a, Code: 65 Extended: 65 Key: 0, Code: 48 Extended: 48 Key: ß, Code: 0 Extended: 16777439 Key: ?, Code: 0 Extended: 16777439 Key: ü, Code: 0 Extended: 16777468 Key: ö, Code: 0 Extended: 16777430 Key: ä, Code: 0 Extended: 16777412
    – DroiDar Dec 29 '14 at 14:32
  • I solved it ...through 8 Corners :-D Thx for your ideas. I dont use the KeyEvent from Java a have an external Method who gives me back a int Value for every Key i press, even when the Focus on the Program is gone. So i detected which Keys are pressed. When it matched for my Cases i save it and use NumPad Codes for typing it later. – DroiDar Jan 02 '15 at 19:05

4 Answers4

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.

  • 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 '19 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.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • > 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 '19 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. – Aaron Digulla Aug 12 '19 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
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    > The `ß` character is such a character. That's factually not true. On German keyboards it *does* appear on the primary layer. Java misses a key constant here. That `VK_BACK_SLASH` appears to work is just a workaround. Backslash is actually not the primary layer of that key. – stewori Jul 25 '19 at 21:21
  • Okay, I found there are actually some open bugs in Java similar to this: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4760902 https://bugs.openjdk.java.net/browse/JDK-8152835 – stewori Jul 25 '19 at 21:59
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.

Tayyab Kazmi
  • 376
  • 4
  • 19
  • `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 '14 at 20:33
  • Could you elaborate a bit – Tayyab Kazmi Dec 29 '14 at 21:08