mjekov
mjekov

Reputation: 682

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.

Upvotes: 1

Views: 4197

Answers (2)

Jason Braucht
Jason Braucht

Reputation: 2368

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();
        }
    }
}

Upvotes: 6

Stephen C
Stephen C

Reputation: 719239

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 ...

Upvotes: 0

Related Questions