What's the keycode of the square brackets

浪尽此生 提交于 2019-12-11 07:37:24

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/15362927/whats-the-keycode-of-the-square-brackets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!