Input handling with polling
In the Saxion Game App, input comes from the keyboard or mouse. You can check input in two ways:
- Polling – ask every frame if a key/button is pressed.
- Events – receive notifications when something happens (like a key press or mouse scroll).
This chapter focuses on polling, which is the most common way in games. The idea of polling is that you check the current state each frame inside the render() method.
Please note: you can use both strategies at the same time. This is sometimes necessary.
Receiving keys by using events is explained in the next chapter.
What to use when?
| Feature | Polling | InputProcessor (Events) |
|---|---|---|
| How it works | Ask every frame if a key or button is pressed (GameApp.isKeyPressed(...)) |
LibGDX calls your methods when something happens |
| Use cases | Continuous movement (holding down a key), real-time game logic | One-time actions, text input, mouse clicks, drag-and-drop |
| Pros | Simple for constant checks | No need to check every frame; reacts immediately |
| Cons | You must manually track states | We need to override method and logic is outside the render() method |
Keypresses
isKeyPressed(int keycode)
Returns true as long as the key is held down. It is continuous: it stays true every frame while the key is down.
Good for movement or holding actions (e.g., walking while holding W).
isKeyJustPressed(int keycode)
Returns true only on the exact frame the key is first pressed. It is triggered once and then returns false until you release and press again.
Good for single actions like jumping, shooting, opening menus, etc.
if (GameApp.isKeyJustPressed(Input.Keys.SPACE)) {
System.out.println("SPACE was just pressed THIS FRAME");
}
Keycodes
We are using the class Input.Keys from LibGDX to select the keycode that we are looking for. For example, in case we want to validate if the user pressed the up arrow on the keyboard, we can use Input.Keys.UP.
The game app is able to detect multiple keys at the same time. So if we want to handle combinations like Shift + R, the code would look like this:
if (GameApp.isKeyPressed(Input.Keys.SHIFT_LEFT) && GameApp.isKeyJustPressed(Input.Keys.R)) {
System.out.println("Both keys were pressed at the same time");
}
Mouse events
Getting the mouse position
There are two possible ways of getting mouse positions in your application. It depends on what you need: the mouse coordinates in your defined world (as world coordinates) or the raw position of the mouse in the window (screen coordinates).
In most cases you will use world coordinates, so that your application is properly scalable. Each ScalableGameScreen has methods available for converting you mouse position to world coordinates. These methods are directly available in your implementation of a screen. So you don't have to call them via GameApp.somemethod(). Instead, you should call the methods getMouseX() and getMouseY().
If you want to receive the raw mouse positions in the window, that you should use the methods from the GameApp. These methods give you the raw position of the mouse in the window where top-left is coordinate (0, 0).
int mouseXRaw = GameApp.getMousePositionInWindowX();
int mouseYRaw = GameApp.getMousePositionInWindowY();
Detecting mouse pressed (and held)
if (GameApp.isButtonPressed(Input.Buttons.LEFT)) {
System.out.println("Left mouse button is being held");
}
if (GameApp.isButtonPressed(Input.Buttons.RIGHT)) {
System.out.println("Right mouse button is being held");
}
if (GameApp.isButtonPressed(Input.Buttons.MIDDLE)) {
System.out.println("Middle mouse button is being held");
}
Detecting Mouse Just Clicked (One-Time Action)
This works exactly like isKeyJustPressed() but for mouse buttons. It returns true only on the frame the button is pressed.
if (GameApp.isButtonJustPressed(Input.Buttons.LEFT)) {
System.out.println("Left mouse button was just pressed");
}
if (GameApp.isButtonJustPressed(Input.Buttons.RIGHT)) {
System.out.println("Right mouse button was just pressed");
}
if (GameApp.isButtonJustPressed(Input.Buttons.MIDDLE)) {
System.out.println("Middle mouse button was just pressed");
}
Mouse button constants
Similarly to how the keycodes are being used, we can also use the mouse button constants in Input.Buttons:
| Button | Constant |
|---|---|
| Left Mouse | Input.Buttons.LEFT |
| Right Mouse | Input.Buttons.RIGHT |
| Middle Mouse | Input.Buttons.MIDDLE |
Example
@Override
public void render(float delta) {
super.render(delta);
// Keyboard
if (GameApp.isKeyPressed(Input.Keys.W)) {
System.out.println("W is being held!");
}
if (GameApp.isKeyJustPressed(Input.Keys.SPACE)) {
System.out.println("Jump!");
}
// Mouse buttons
if (GameApp.isButtonPressed(Input.Buttons.LEFT)) {
System.out.println("Left mouse button is held");
}
if (GameApp.isButtonJustPressed(Input.Buttons.RIGHT)) {
System.out.println("Right mouse button just clicked");
}
// Mouse position in world coordinates
float mouseX = getMouseX();
float mouseY = getMouseY();
System.out.println("Mouse position in the world: " + mouseX + ", " + mouseY);
// Mouse position in screen coordinates
int screenX = GameApp.getMousePositionInWindowX();
int screenY = GameApp.getMousePositionInWindowY();
System.out.println("Mouse position on the screen: " + screenX + ", " + screenY);
}