The Coder's Handbook
Mouse And Keyboard Events
METHOD #1 - EVENT RESPONSE
You can find a more detailed and direct reference for this in this Input Article in the Slick2D Wiki. This includes information about gamepad support. You can also read up on the the KeyListener and MouseListener classes in the Slick 2D Javadoc
Concept
The first method for getting input is by using a set of methods from BasicGameState that are called automatically, such as keyPressed() and mousePressed().
The game engine uses an event listener to wait for something to happen. These methods are event handlers which execute when the triggered event occurs (once per frame).
To use them you simply override the method
These are easy to use and work well for system-level events that aren't sensitive to timing.
Key Press and Release
There are two different methods that deal with the keyboard
void keyPressed(int key, char c) // Invoked when the key is first pressed down
void keyReleased(int key, char c) // Invoked when a key is first released
Both methods operate in the same manner. You are given two possible parameters to use:
A integer named key which corresponds to a key code
A char named c that notes which letter was pressed
Really, you only need to use key but it can be handy to use the character in some cases too. To get access to the codes, you'll use constants defined in the Input class.
public void keyPressed(int key, char c)
{
if (key == Input.KEY_1)
{
System.out.println("You pressed 1.");
}
if (key == Input.KEY_SPACE)
{
System.out.println("You pressed the space bar.");
}
if (key == Input.KEY_P)
{
System.out.println("You pressed p");
}
if (c == 'm')
{
System.out.println("You pressed m");
}
}
Mouse Events
There are four different methods that deal with the mouse
void mousePressed(int button, int x, int y) // Invoked when the mouse is first pressed down
void mouseReleased(int button, int x, int y) // Invoked when the mouse is first released
void mouseMoved(int oldx, int oldy, int newx, int newy) // Invoked when the mouse is moved.
void mouseClicked(int button, int x, int y, int clickCount) // Invoked when the mouse is clicked. Avoid unless using double clicks (see Javadoc).
The most commonly used of these will be the mousePressed method, which is similar to a keyPress.
public void mousePressed(int button, int x, int y)
{
// If the left mouse button is pressed and it is inside the box, increase score
if(button == 0 && x >= 200 && x <= 400 && y >= 200 && y <= 400)
{
score++;
}
}
METHOD #2 - POLLING
Concept
The second option is to directly poll the keyboard or mouse to find out its current status
This can be useful when you want to manage behavior inside a class or need to know the exact state of a key at a given moment.
This can be helpful for when you want to do an event while a button is being held down.
Keyboard
You can check if key is down by using the input object in the GameContainer.
// If the space key is down call the shoot() method
if(gc.getInput().isKeyDown(Input.KEY_SPACE))
{
shoot();
}
Mouse
You can check the mouse state directly by using he input object in the Game Container.
// Check if the mouse is down. 0 is left, 1 is middle, 2 is right
if(gc.getInput().isMouseButtonDown(0))
{
System.out.println("Click!");
}
// Print out the current X and Y positions of the mouse
System.out.println(gc.getInput().getMouseX() + ", " + gc.getInput().getMouseY())
Do your parents believe you when you tell them you need the glowing RGB for programming?
RESOURCES
The New Boston - Mouse Input
The New Boston - Keyboard Input