1

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.

MByD
  • 135,866
  • 28
  • 264
  • 277
mjekov
  • 682
  • 2
  • 17
  • 32

2 Answers2

6

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();
        }
    }
}
Jason Braucht
  • 2,358
  • 19
  • 31
0

The general solution is to call KeyEvent.getExtendedKeyCodeForChar(int c). If the unicode codepoint c has a VK_ constant that will be returned. Otherwise a "unique integer" is returned.

I think that '`' is KeyEvent.VK_BACK_QUOTE ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216