| name | fxgl-input |
| description | Handle all input in FXGL — bind keyboard keys (held, press, release), mouse buttons and position, virtual on-screen joystick and controller buttons, hardware gamepad via fxgl- controllerinput, capture and replay input recordings, register input combos/sequences, and use the fluent input builder. Use this skill when wiring player controls, implementing key rebinding, adding mobile touch controls, recording demos, or implementing fighting-game combo systems.
|
| triggers | ["keyboard input","mouse input","onKey","onKeyDown","UserAction","virtual joystick","controller","input binding","key combo","input capture"] |
| compatibility | Java 17+, FXGL 21.x. Gamepad support requires fxgl-controllerinput module.
|
| category | fxgl/input |
| tags | ["fxgl","java","javafx","input"] |
| metadata | {"author":"fxgl-skills","version":"1.0","fxgl-version":"21.1"} |
| allowed-tools | ["Read","Write","Edit","Bash"] |
FXGL Input System
Overview
All input bindings go in initInput(). Actions fire on the game thread synchronised with
the game loop — no threading concerns.
Keyboard — Three Action Phases
@Override
protected void initInput() {
onKey(KeyCode.A, () -> player.getComponent(PC.class).moveLeft());
onKey(KeyCode.D, () -> player.getComponent(PC.class).moveRight());
onKey(KeyCode.W, "Move Up", () -> player.getComponent(PC.class).moveUp());
onKeyDown(KeyCode.SPACE, () -> player.getComponent(PC.class).jump());
onKeyDown(KeyCode.ESCAPE, () -> getGameController().gotoGameMenu());
onKeyUp(KeyCode.SHIFT, () -> player.getComponent(PC.class).stopSprint());
}
UserAction — Full Control Over All Three Phases
Use UserAction when a single binding needs begin, held, and end logic (e.g., sprint):
getInput().addAction(new UserAction("Sprint") {
@Override
protected void onActionBegin() {
player.getComponent(PC.class).startSprint();
}
@Override
protected void onAction() {
player.getComponent(PC.class).sprint();
}
@Override
protected void onActionEnd() {
player.getComponent(PC.class).stopSprint();
}
}, KeyCode.SHIFT);