如何在java中检测箭头键? [英] how can I detect arrow keys in java?

查看:438
本文介绍了如何在java中检测箭头键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何实现一个关键的监听器;这不是问题。

I know how to implement a key listener; that's not the problem.

public void keyTyped(KeyEvent event) {
    if (event.getKeyChar() == KEY_LEFT) {
        cTDirection = LEFT;
    }
    if (event.getKeyChar() == 40) {
        cTDirection = DOWN;
    }
    if (event.getKeyChar() == 39) {
        cTDirection = RIGHT;
    }
    if (event.getKeyChar() == 38) {
        cTDirection = UP;
    }
}

我把什么放在 LEFT_KEY / 40/39/38?当我创建一个keylistener并键入键时,我相信我得到了37 - 40.我不知道该把它放在那里只听箭头键。

What do I put where the LEFT_KEY / 40 / 39 / 38? When I created a keylistener and type in the keys, I believe I got 37 - 40. I don't know what to put there to listen for just the arrow keys.

推荐答案

我建议使用:

if (event.getKeyCode() == KeyEvent.VK_UP) {
...
}

以<$ c重复$ c> VK_DOWN,VK_LEFT,VK_RIGHT 。

数字小键盘有单独的代码: VK_KP_UP,VK_KP_DOWN, VK_KP_LEFT,VK_KP_RIGHT 如果您需要它们。

There are seperate codes for the numeric keypad: VK_KP_UP, VK_KP_DOWN, VK_KP_LEFT, VK_KP_RIGHT if you need them.

参见 KeyEvent

这篇关于如何在java中检测箭头键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆