Keyboard input

Under Linux I receive multiple keyPressed & keyReleased events whilst a key is held down. Do any other operating systems exhibit this behaviour? I am considering working around this issue by detecting if the OS is Linux and then only considering a key as released after a given duration has elapsed and no new keyPressed events have arrived for the key. Is this likely to be a reliably work around? I imagine it will work perfectly under my setup however I am unsure how consistent the rate of arrival of keyPressed / keyReleased events is among other setups. I have previously used JInput however if I recall I had to set some permissions on my system for it to work correctly. I’d prefer to avoid going down this path if at all possible.

Thanks :slight_smile:

Has that behaviour still not been fixed?! I remember that bug being in Java 1.1! :-\

Just forget Linux ::slight_smile:

‘Please keep the ‘A’ key pressed down for 3…2…1…seconds. Thank you. We have now calibrated your keyboard. Please have a safe gaming experience.’

I would but it’s my OS of choice, it’d be nice to play my own game every now and then :wink: I think I’ll probably go down the calibration route as Karmington suggests. Do any another operating systems exhibit this behaviour, or is it just Linux?

It’s been a while, but I think that the timestamps (InputEvent.getWhen()) of the extraneous keyUp/keyDown pairs have the same value, so you can filter them out.

Are you looking to perform some action on keyReleased, or is it sufficient just to maintain a boolean variable tracking key state? I’ve never seen problems with this.

Thanks for the input ryanm, I’ll take a look into getWhen(). I was actually looking to perform a game event only once no matter how long the key is held down for. If the key is released and then repressed this should trigger a second game event, etc. I think I pretty much solved the problem – at least its working correctly on the three different Linux setups I run. Basically on Linux I maintain my own ‘key state’. When a keyPressed event is received the associated key state is set to indicate pressed. When a keyReleased event is received a countdown timer is started, however if queried the key state is still considered pressed. If a new keyPressed event is received for the key before the timer expires the timer is cancelled. If the timer expires the associated key state is set to indicate released. Obviously you lose some granularity in that a key may be considered pressed for a little time after release, however for my game at least this is not noticeable in the slightest.

This has long been a painful problem on Linux that absolutely should have been fixed but was marked as not a bug. The reasoning being that KeyPressed/Released/Typed events are thin wrappers around OS events and this is just the way X11 does key events. This of course totally screws you if you need super-responsive key handling. The solution you describe is the only reliable one available without resorting to using a native lib.