Search code examples
javakeyeventkeycode

What's the keycode of the square brackets


I'm trying to use the Robot class in Java and type some text. Unfortunately I'm having problems finding the key codes of the square brackets, this symbol | and this symbol `. I can't find them in the KeyEvent constants. I want to use them, because the text i'm typing is in cyrillic and these symbols represent characters in the alphabet. Thanks in advance.


Solution

  • It's in the JavaDoc for KeyEvent

    KeyEvent.VK_OPEN_BRACKET

    and

    KeyEvent.VK_CLOSE_BRACKET

    Edit

    From the KeyEvent JavaDoc

    This low-level event is generated by a component object (such as a text field) when a key is pressed, released, or typed.

    So on a US 101-key keyboard, the ` and ~ will produce the same keycode, although ~ will have a SHIFT modifier. Also notice that KeyEvent.VK_BACK_SLASH traps the | (pipe) keystroke too.

    Try adding the following sample KeyAdapter to your project to see this in action.

    new KeyAdapter()
    {
        public void keyPressed(final KeyEvent e)
        {
            if (e.getKeyCode() == KeyEvent.VK_BACK_QUOTE)
            {
                e.toString();
            }
            if (e.getKeyCode() == KeyEvent.VK_BACK_SLASH)
            {
                e.toString();
            }
            if (e.getKeyCode() == KeyEvent.VK_OPEN_BRACKET)
            {
                e.toString();
            }
        }
    }