java 箭头键代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15378193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codes for Arrow Keys
提问by Rohan
In a java program I've added keylistener and I want to check if anyone has pressed the arrow keys or not.Can anyone please help me since I dont know any ascii codes for arrow keys? If possible please give a sample program also.
在 Java 程序中,我添加了 keylistener,我想检查是否有人按下了箭头键。有人可以帮助我,因为我不知道箭头键的任何 ascii 代码吗?如果可能,还请提供示例程序。
回答by Breavyn
KeyEvent.VK_UP
KeyEvent.VK_DOWN
KeyEvent.VK_LEFT
KeyEvent.VK_RIGHT
KeyEvent.VK_UP
KeyEvent.VK_DOWN
KeyEvent.VK_LEFT
KeyEvent.VK_RIGHT
These are the conditions that you test e.getKeyCode()
for.
这些是您测试的条件e.getKeyCode()
。
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch( key ) {
case KeyEvent.VK_UP:
// up
break;
case KeyEvent.VK_DOWN:
// down
break;
case KeyEvent.VK_LEFT:
// left
break;
case KeyEvent.VK_RIGHT :
// right
break;
}
}
EDIT: I just see now a duplicate of this question here How to check if the key pressed was an arrow key in Java KeyListener?
编辑:我现在刚刚在这里看到这个问题的副本如何检查按下的键是否是 Java KeyListener 中的箭头键?