KeyListener limit update for Java

So, I have a KeyListener added to my JFrame, and Thread to my JPanel, which is added to my JFrame. The problem is that the thread is replicating more slowly than the keylistener notification ... is there a way to limit the keylistener to 4 keys per second (or something else).

+4
source share
1 answer

You can use the Timer in the KeyListener or just follow the last keystroke and compare it with the current time.

This allows you to exit KeyListener before any of your logic tasks are achieved. However, you cannot stop KeyListener starting KeyListener you permanently delete it and add it again after 250 ms.

Also note that in Swing, you usually used key bindings instead of KeyListener , although they would have the same problem.

+4
source

Source: https://habr.com/ru/post/1436059/


All Articles