问题
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