Java中的Stop Windows键输入延迟 [英] Stop windows Key Input Delay in Java

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

问题描述

我正在创建一个侧滚动游戏,当前当我按下箭头键时,角色会移动,暂停然后无限期移动,直到释放键为止.

I am creating a side scrolling game and currently when I press an arrow key, the character moves, pauses then moves indefinitely until the key is released.

暂停来自Windows的按键延迟"配置,因此,如果您按住按键的时间过长,就不会意外键入重复的按键.

The pause comes from Windows configuration with Key Delay, so you don't accidentally type in duplicate key presses if you hold down the key too long.

我想知道是否有办法摆脱这种情况.

I want to know if there is a way to get rid of this.

这是我按键的代码:

public void keyReleased(KeyEvent ke){}

public void keyTyped(KeyEvent ke){}

public void keyPressed(KeyEvent ke){
    int code = ke.getKeyCode();

    if(code == KeyEvent.VK_UP){
        if(playerY > 0){
            playerY-=speed;
            repaint();
        }
    }

    else if(code == KeyEvent.VK_DOWN){
        if(playerY < 600){
            playerY+=speed;
            repaint();
        }
    }

    else if(code == KeyEvent.VK_RIGHT){
        if(playerX < 800){
            playerX+=speed;
            repaint();
        }
    }

    else if(code == KeyEvent.VK_LEFT){
        if(playerX > 0){
            playerX-=speed;
            System.out.println(playerX);
            repaint();
        }
    }
}

推荐答案

按下键时,应设置一些变量来确定运动.在KeyReleased上,您无需设置此变量.

When the key is pressed, you should set some variable by which you determine movement. On KeyReleased you unset this variable.

如何处理该方法以调用方法,例如moveLeft(),并在循环中检查此变量.在KeyReleased上,当您重新定义它时,循环结束.

The way how you handle this it to call the method, for example moveLeft(), and in loop you are checking this variable. On KeyReleased, when you redefine it, the loop ends.

这篇关于Java中的Stop Windows键输入延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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