Problem with key binding

Hello, i use this code to know wich key has been pressed in my game :

public void update(float tpf) {

super.update(tpf);

if (KeyBindingManager.getKeyBindingManager().isValidCommand("keyRight", false)) {

this.keyArrowEvent(1);

}

else if (KeyBindingManager.getKeyBindingManager().isValidCommand("keyLeft", false)) {

this.keyArrowEvent(-1);

}

else if (KeyBindingManager.getKeyBindingManager().isValidCommand("keyReturn", false)) {

this.keyReturnEvent();

}  

}

The problem is, if i pressed the keyright, the function keyarrowevent(1) is called at least 5 or 6 times, but this does not happen with the other two functions.

But the most strange thing is, if i change the order of the if… else if…, the first condition is executed many times (like i already said). What could be the problem, i have already tried use other keys, like M, but nothing changed. Thank you.

I'm not sure - but I'm curious, so what happens if you remove the "else" statements.


if (KeyBindingManager.getKeyBindingManager().isValidCommand("keyRight", false)) {
   this.keyArrowEvent(1);
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("keyLeft", false)) {
   this.keyArrowEvent(-1);
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("keyReturn", false)) {
   this.keyReturnEvent();
}

 

Is it still only the first one listed that is executed many times?

What is in your keyArrowEvent() and keyReturnEvent() methods?

If i remove the else statements it works fine. Why this? Could anyone explain me why this problem?

I don't really know … but if you think about it having the else statements would mean that if the first key is held down then the other keys won't react - which probably isn't desirable anyway.

Ok thank your for your answer.