3

I have written code that detects which key that has been pressed.

@Override
        public void keyPressed(KeyEvent e) {
            int code = e.getKeyCode();
            String name = KeyEvent.getKeyText(code);

            tArea.append(name + " pressed\n");
        }

This code works fine. But on my keyboard, that is a circle that can be used to stop a song that is playing in mediaplay, start the song (and pause the song as well), change to next song, and go back to previous song. Many keyboards have this button. But when I press a button from this circle, it says:

Unknown keycode: 0x0 pressed.

Is there a way I can get this key? I need to know what it is, since I am writing a program that should automatically press on of these keys.

Thanks in advance

States
  • 141
  • 11
  • 2
    http://stackoverflow.com/questions/17255549/how-can-i-get-the-keyboard-scan-code-in-java is probably relevant. you need the scancode. those various specialty keys are may not be mapped to a specific "character". so they'll have a scancode, but no keycode. – Marc B Dec 23 '14 at 15:52
  • i think the most important key's which is usable for programmers have special code numbers,and for other keys i don't know!maybe you have to make it by yours(if i was,try to find the way and use switch case for detect more buttons!) – Mohammadreza Khatami Dec 23 '14 at 16:00

1 Answers1

3

You need to use the getExtendedKeyCode() method to get hold of these key events.

Note that the Javadoc says that you won't get a KEY_TYPED event for these, but only a KEY_PRESSED and KEY_RELEASED, because KEY_TYPED is for things that can be turned in to Unicode characters, which this can't.

If the extended key code still isn't enough to identify what you're pressing, you'll need to resort to the scan code, which is present in the KeyEvent but not accessible because it's a private field. There's a nice little bit of reflection magic in this answer that will allow you to get at the scan code.

This list of key codes might help you. You're dealing with something in the "non-mappable" list.

Community
  • 1
  • 1
chiastic-security
  • 20,430
  • 4
  • 39
  • 67