keyreleased bug

Hi.
I am developing a game in Java.
When I keep a directional key pressed (for my sprite in game keep running) sometimes an event keyreleased is thrown but I did not released the key! It occours in Linux but not in Windows…
I have found this link about the subject:

http://developer.java.sun.com/developer/bugParade/bugs/4338703.html

Does anyone knows how to overcome this problem?

Thanks,
Luiz Fernando

erm…

“It occours in Linux but not in Windows…”

did u tryed that with the same keyboard?

alternativly u could simply try another way to handle inputs:


implements KeyListener
...
addKeyListener(this);
...
public void keyPressed(KeyEvent ke)
{controls[ke.getKeyCode()&0xFF] = 1;}

public void keyReleased(KeyEvent ke)
{controls[ke.getKeyCode()&0xFF] = 0;}

public void keyTyped(KeyEvent ke){}

goes to :-


enableEvents(AWTEvent.KEY_EVENT_MASK);
...
public void processKeyEvent(KeyEvent ke)
{controls[ke.getKeyCode()&0xFF] = (ke.getID()==KeyEvent.KEY_RELEASED)?0:1 ;}

that asumes, that u store the keystates in a int-array wich is capable to hold 256 values

int[] controls=new int[256];

btw the code snippets r from abuse :slight_smile:

Yes it is the same keyboard. Actually is the same machine. I have dual boot (Linux and Windows).
Instead of having such big array I have just 2 boolean variables (goingRight and goingLeft).
In KeyPressed I verify what key the user is pressing and then a set the variable to true and in the KeyReleased event I set the variable to false.
In WIndows enviroment works ok, but in Linux sometimes occur a keyreleased event even tough I have not released the key!
I have seen the bug report (see the link in my first post) in the sun homepage but it is matter of the X/Windows enviroment of Unix like OS
But I have to overcome this problem…
Any suggestions?

well this wouldn’t be pretty but what about checking for a specified period of time after the keyReleased event before resetting the boolean.

So you get a key pressed set the boolean to true. When you get a keyReleased start the timer. If you get another keyPressed then kill the timer. In your rendering loop check to see if the timer has gone past a specified amount of time, this means you didn’t get antoher keyPressed after the keyReleased so it must have been a real keyRelease event.