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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:22:57  来源:igfitidea点击:

Codes for Arrow Keys

java

提问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_UPKeyEvent.VK_DOWNKeyEvent.VK_LEFTKeyEvent.VK_RIGHT

KeyEvent.VK_UPKeyEvent.VK_DOWNKeyEvent.VK_LEFTKeyEvent.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 中的箭头键?