This
AWTEventListener
tries to work around
a 12 yo
bug in the Linux KeyEvent handling for keyboard repeat. Linux apparently implements repeating keypresses by
repeating both the
KeyEvent.KEY_PRESSED
and KeyEvent.KEY_RELEASED
, while on Windows, one only
gets repeating PRESSES, and then a final RELEASE when the key is released. The Windows way is obviously much more
useful, as one then can easily distinguish between a user holding a key pressed, and a user hammering away on the
key.
This class is an
AWTEventListener
that should be installed as the application's first ever
AWTEventListener
using the following code, but it is simpler to invoke
install(new
instance)
:
* Toolkit.getDefaultToolkit().addAWTEventListener(new RepeatingReleasedEventsFixer
, AWTEvent.KEY_EVENT_MASK);
Remember to remove it and any other installed
AWTEventListener
if your application have some "reboot"
functionality that can potentially install it again - or else you'll end up with multiple instances, which isn't too
hot.
Notice: Read up on the
RepeatingReleasedEventsFixer.Reposted
interface if you have other AWTEventListeners that resends KeyEvents
(as this one does) - or else we'll get the event back.
Mode of operation
The class makes use of the fact that the subsequent PRESSED event comes right after the RELEASED event - one thus
have a sequence like this:
* PRESSED
-wait between key repeats-
RELEASED
PRESSED
-wait between key repeats-
RELEASED
PRESSED
etc.
A timer is started when receiving a RELEASED event, and if a PRESSED comes soon afterwards, the RELEASED is dropped
(consumed) - while if the timer times out, the event is reposted and thus becomes the final, wanted RELEASED that
denotes that the key actually was released.
Inspired by
http://www.arco.in-berlin.de/keyevent.html