I’m developing a small 2D game engine in Java, after playing around with my demo game in a VirtualBox VM hosting Ubuntu, I found a strange bug that would sometimes cause the game to ignore the fact that a key is pressed. So you’re running to the left until you suddenly stop moving.
Now under a real Ubuntu I found the cause of the problem. When I hold a key the keyPress/keyRelease events are send all the time.
My system to check for pressed keys is the following:
– if a key gets pressed add it to the “downlist”
– if a key is released add it to the uplist
– on each frame of the game remove the keys in the uplist from the downlist
– if a key is still in the downlist it’s pressed
Now when you press a second key, sometimes keyRelease was the last event fired by the other key which is still held but not recognized in that way.
Any ideas how to fix this? It’s really annoying.
EDIT
For clarification this is the result I get when holding down a key continuously:
pressed: 87
released: 87
released: 87
pressed: 87
released: 87
pressed: 87
released: 87
pressed: 87
released: 87
etc.
EDIT2
Ok after googling a bit more I found out that this is a “feature” of the X11 server, but I still have no clue how to detect the “fake” key events in java.
Okay… I “fixed” it.
Since X11 keeps firing it’s auto repeat key events, there’s no way to change the whole thing in a way that those “fake” events are ignored, you can’t distinguish between “real” and “fake” events in Java.
So the way to fix it is the following. Since every “fake” keyUp event is followed by an immediate keyDown event, you simply remove the keyUp event from the keyRemoveList if you receive a keyDown event this looks like the following:
Since the “real” keyUp event isn’t followed by an immediate keyDown event it’s processed normally. In theory it should be impossible for a game frame to occur between a “fake” keyUp and keyDown.