So, I have a KeyListener added to my JFrame, and a Thread to my JPanel which is added to my JFrame. The problem is that the thread repaints slower than the notification of keylistener…is there a way I can limit the keylistener to 4 keys per second(Or whatever).
Share
You could use a
Timerin theKeyListener, or simply keep track of the last time a key was pressed and compare that with the current time.This allows you to quit early from the
KeyListenerbefore any of your logic is reached. You can however not stop theKeyListenerfrom being triggered, except when you constantly remove it and add it again after 250 ms.Also note that in Swing you typically would use key bindings instead of a
KeyListener, although they would have the exact same problem.