Slick 2D How to check if the user is hitting a single key

advertisements

I'm working on a 2D Game, and I have a problem. I'm working with Slick 2D. I can check which key is the user hitting. But I don't know how to check if the user is pressing nothing. I want the game to know that if I press the up key and the right key, it shall move in the direction. I already programmed all the multiple key stuff input.

How do I tell slick 2D: If the user has hit only one Key then something should happen.

The input is defined as :

Input input1 = gc.getInput();

in my update Method.


Quick and dirty solution is to create a method to check for the particular keys that you want to know are not being pressed down at the moment, for example the arrow keys:

private boolean areKeysPressed(Input input) {
if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_RIGHT) || input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_DOWN)) return true;
else return false;
}

And then in your update() method you can do the following:

if (!areKeysPressed(input1)) {
//code for what you want to happen if none of the arrow keys are pressed
}