修复Java按键操作中的延迟问题 [英] Fixing delay issue in java keypress action

查看:79
本文介绍了修复Java按键操作中的延迟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用键侦听器的Java应用程序.我注意到,当用户按住键时使用按键监听器时,它会注册,然后花一秒钟的时间来检测保持状态.有办法摆脱这种短暂的延迟吗?

I am creating a Java application that uses the keylistener. I have noticed that when using the keypressed listener when the user is holding down the key, it registers and then takes a second to detect the hold. Is there a way to get rid of this short delay?

public void keyPressed(KeyEvent arg0) 
{
    char key = arg0.getKeyChar();
    if(key == 'a')
        update = Status.Left;
    else if(key == 's')
        update = Status.Crouch;
    else if(key == 'd')
        update = Status.Right;
    else if(key == ' ')
        update = Status.Shoot;
    else
        update = Status.None;
    System.out.println(arg0.getKeyChar());
    this.repaint();
}

推荐答案

我建议您不要使用keyPressed事件检测到保持状态.使用计时器不断检查按键是否被按下.使用一个BitSet.在keyPressed和keyReleased上更新BitSet以跟踪按下的键.这样,您可以一次检测多个按键. 如果您要开发游戏,那么您已经拥有可以用于此目的的游戏时钟

I suggest you don't detect the hold using the keyPressed event. Use a timer to constantly check for pressed keys. Use a BitSet. On keyPressed and on keyReleased update the BitSet to keep track of the pressed keys. This way you can detect multiple key presses at once. If you are building a game you already have the game clock which could be used for this

这篇关于修复Java按键操作中的延迟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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