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.