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.
Robin source share