Java KeyListener keyPressed 方法触发太快

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5199581/
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-30 09:54:50  来源:igfitidea点击:

Java KeyListener keyPressed method fires too fast

javaswingkeylistenerkeyevent

提问by Mark

If you use the java KeyListenerclass you know that if you hold down a key keyPressedwill fire one KeyEvent, and then about half a second later will fire the same key many times very very fast. I would like to know if there is a way to keep the KeyEventsfrom firing too fast. I would like them to be at a nice constant rate of about once every 500 ms.

如果你使用 javaKeyListener类,你就会知道如果你按住一个键keyPressed会触发一个KeyEvent,然后大约半秒后会非常非常快地多次触发相同的键。我想知道是否有办法防止KeyEvents发射过快。我希望它们保持良好的恒定速率,大约每 500 毫秒一次。

回答by Kurt Kaylor

You can, but the trick is to not slow down the firing of events, but to slow down how fast you process them:

你可以,但诀窍是不要减慢事件的触发速度,而是减慢处理它们的速度:

KeyListener kl = new KeyListener() {
    private long lastPressProcessed = 0;

    @Override
    public void keyPressed(KeyEvent e) {
        if(System.currentTimeMillis() - lastPressProcessed > 500) {
            //Do your work here...
            lastPressProcessed = System.currentTimeMillis();
        }           
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {   }

};

回答by aioobe

No, this is completely system dependent. You would have to listen for keyPressedevents, start a timer on your own that fires events at a fixed rate and stop it at the next keyReleasedevent.

不,这完全取决于系统。您必须监听keyPressed事件,自己启动一个计时器,以固定速率触发事件并在下一个keyReleased事件时停止。

Try something like this:

尝试这样的事情:

component.addKeyListener(new KeyListener() {

    Timer t = new Timer();
    TimerTask tt;

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        tt.cancel();
        tt = null;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (tt != null)
            return;

        tt = new TimerTask() {
            @Override
            public void run() {
                System.out.println(System.currentTimeMillis() % 1000);
            }
        };

        t.scheduleAtFixedRate(tt, 0, 500);
    }
});

回答by Matthew Gilliard

This is controlled by your OS. But you could easily have your handler check how long it is since the last time it fired and respond accordingly.

这由您的操作系统控制。但是您可以轻松地让您的处理程序检查自上次触发以来的时间并做出相应的响应。

回答by Ted Hopp

Usually the auto-repeat rate for keys is set by the system; I don't know if it's changeable from within Java. However, you can use the event arrival time to not react to one until 500ms after the last time you reacted (or after a key release, which should clear the timer for users who type fast).

通常按键的自动重复率是由系统设置的;我不知道它是否可以从 Java 内部更改。但是,您可以使用事件到达时间,直到最后一次反应后 500 毫秒(或释放键后,这应该为快速键入的用户清除计时器)才对一个做出反应。