Input handling with events (Advanced)
⚠️ Advanced Topic
This section is intended for students who are comfortable with the basics of object-oriented programming. You should already know how to define classes, create objects, and work with object properties. If you are new to OOP, we recommend you to stick to using the polling methods.
In the previous article you have read about polling, the default way of checking the state of the keys and mouse buttons during the render() method.
This article focuses on the InputProcessor methods. These methods can be used to receive events, when certain actions occur (e.g. user presses a key on the keyboard).
Available methods
If your screen is based on the ScalableGameScreen of the GameScreen class, then there is already an InputProcessor available. All methods are currently hidden, but you can override them. The following methods are available:
| Method | Description |
|---|---|
keyDown(int keycode) |
Called when a key is pressed down. |
keyUp(int keycode) |
Called when a key is released. |
keyTyped(char character) |
Called when a character is typed (good for text input). |
touchDown(int x, int y, int pointer, int button) |
Called when a mouse button or touch is pressed. |
touchUp(int x, int y, int pointer, int button) |
Called when a mouse button or touch is released. |
touchDragged(int x, int y, int pointer) |
Called when a finger or mouse is dragged. |
mouseMoved(int x, int y) |
Called when the mouse moves without pressing buttons. |
scrolled(float amountX, float amountY) |
Called when the mouse wheel is scrolled. |
touchCancelled(int x, int y, int pointer, int button) |
Called when a touch is cancelled (rarely used). |
How It Works
- LibGDX calls the appropriate method automatically when the event happens.
- You don’t need to poll for the input in render().
Example: Moving a player left or right using events:
@Override
public boolean keyDown(int keycode) {
if(keycode == Input.Keys.LEFT) player.setVelocity(-100, 0);
if(keycode == Input.Keys.RIGHT) player.setVelocity(100, 0);
return true;
}
@Override
public boolean keyUp(int keycode) {
if(keycode == Input.Keys.LEFT || keycode == Input.Keys.RIGHT) player.setVelocity(0, 0);
return true;
}
Collecting text input
A common use case for events is, if you are interested in text that is being typed. It is impossible to collect this in the render loop. The InputProcessor however, is able to do this.
Here’s a simple way using keyTyped:
public class TextInputScreen extends ScalableGameScreen {
private String currentText = "";
... // the other methods
@Override
public void render(float delta) {
GameApp.clearScreen("black");
GameApp.drawText("main", "Enter your name: " + currentText, 100, 300, Color.WHITE);
}
@Override
public boolean keyTyped(char character) {
if(character == '\b') { // Backspace
if(currentText.length() > 0) {
// Remove the last character
currentText = currentText.substring(0, currentText.length() - 1);
}
} else if(character == '\r' || character == '\n') { // Enter characters
submitName(currentText);
} else {
// Add the typed character
currentText += character;
}
return true;
}
private void submitName(String name) {
System.out.println("Name entered: " + name);
// Here you could save it to a highscore table
}
}
Notes
- keyTyped is better for text input than keyDown, because it respects characters and keyboard layout.
- Use keyDown and keyUp for continuous actions like movement.
- You can combine polling and InputProcessor if needed. For example, polling for movement and events for menu selection.
- Always return true from the event method if you handled the input; otherwise LibGDX might pass it to other processors.