I am using slick2d to implement an asteroids-like game. In the update method I am listening to the up,right and left buttons, and if pressed I am moving the spaceship around. I am also listening to the space button, which when pressed causes bullets to appear. Problem is when any two keys are pressed together for example the up button & the space button everything works fine: the spaceship moves forward and bullets starts firing; BUT the moment when more than two buttons are pressed simultaneously, the last button to be pressed is ignored. Example if the up button, the left button and the space button are pressed: the spaceship moves in vertically and to the left but does not fire any bullets. Is this a common problem when implementing games and do any techniques exist so as to address simultaneous keyboard/gamepad input. Thank you very much. DB.
Behold pseudo-code.
private ArrayList buttonsPressed = new ArrayList();
onButtonPressed(int keycode)
{
if (!buttonsPressed.contains(keycode))
{
buttonsPressed.add(keycode);
}
}
onButtonReleased(int keycode)
{
buttonsPressed.remove(keycode);
}
respondToKeyboardInput()
{
for (int i = 0; i < buttonsPressed.size(); i++)
{
if (buttonsPressed.get(i) == Key.LEFT)
{
goLeft();
}
else if (buttonsPressed.get(i) == Key.JUMP)
{
jump();
}
//etc
}
}
=) Wonderful!! A thousand thanks. ;D
Hi, Sorry to ask again. Could not correctly detecting multiple keyboard input be a hardware problem rather than a software problem? I implemented my input handling as indicated in the above pseudocode and it works fine but not all the time. What I did was to output the id of the key pressed to standard output in the onButtonPressed() method. What happens is that when two buttons are pressed at the same time their id number is outputted but when a third button is added to the combination - its number will not always output. So if the onButtonPressed() method is supposed to catch all instances when a button is pressed, can it be assumed that not all keyboards register all the keys that are pressed concurrently? - thanks again.
Certain keyboards do have problems where combinations of 3 keys incorrectly report presses, other keys, or don’t respond at all. Look into an explanation of key jamming and ghosting here to see if maybe this is the case: http://en.wikipedia.org/wiki/Rollover_(key)
As an example on my Mac’s keyboard, I can hold Q, W, F and then the R key is completely ignored and won’t trigger any press events.
I do it this way, in the update method
if (input.isKeyDown(Input.KEY_LEFT)) { // Move player left
// Moving left..
} else if (input.isKeyDown(Input.KEY_RIGHT)) { // Move player right
// Moving the player right..
} else { // No movement
// Reset animations
}
if (input.isKeyDown(Input.KEY_SPACE)) { // Jumping
// Jumping stuff
}
So that everytime you dont care if anything else is pressed, you can just make a new if-statement for it. Kind of self-explainatory.
You can grab input through the container btw.
Meh… forum messed up my tabbing :L
Thanks everyone for replying ;D I finally think to have managed to find a solution. Rather than using the letter keys from the keyboard (A,B,C,D,E,F…and SPACE) when simultaneous key presses are required use keys such as ctrl or alt in combination with any other two keys. These mentionned keys are wired differently from the rest of the keys (to keep explanations simple) and seem to be more reliable when multiple keys need to be detected if pressed simultaneously. At least, this works with my keyboard anyway. All three keys (alt,ctrl and any two directional arrows) register when pressed together. Thanks again :point: 8)
3 keys should register fine, but 4 is pushing it. Like you said, modifier keys are often differently wired, so they should not count towards this limit. I’ve never had a problem with this in any game though.