Input and gamepads¶
The input manager maps physical controls to named actions that your game can respond to. Because controllers differ in their axes, buttons and capabilities, use those names to keep game behavior independent of a particular hardware layout.
Enable joystick input with settings.setUseJoysticks(true) in the application settings before starting the game.
Once input is initialized, use inputManager.getJoysticks() to find connected devices, checking whether the result is absent or empty. You can bind each axis to positive and negative mappings with JoystickAxis.assignAxis, then use an AnalogListener for continuous movement and an ActionListener for button actions. Allow for dead zones and differences between devices.
A simple binding after input initialization can use the first available joystick:
Joystick[] devices = inputManager.getJoysticks();
if (devices != null && devices.length > 0) {
JoystickAxis xAxis = devices[0].getXAxis();
if (xAxis != null) {
xAxis.assignAxis("MoveRight", "MoveLeft");
inputManager.addListener((AnalogListener) (name, value, tpf) -> {
// Apply movement for the named direction here.
}, "MoveRight", "MoveLeft");
}
}
Register equivalent keyboard and mouse triggers under the same action names when appropriate.
For a rebinding screen, enumerate each device’s axes and buttons and let the player choose. Replace the relevant mapping and reassign all its desired triggers. Since devices can disconnect and axis conventions differ, provide a way to inspect and change bindings.
The engine example jme3test.input.TestJoystick demonstrates device enumeration, mappings and vibration.
On-screen virtual gamepad¶
The virtual joystick lets users control the game through touch.

The screenshot shows VirtualGamepadDemo.java running on Linux with the virtual gamepad explicitly enabled. On mobile, use automatic mode for the usual behavior. Configure it in AppSettings before starting the app:
settings.setUseJoysticks(true);
settings.setVirtualJoystick(AppSettings.VIRTUAL_JOYSTICK_AUTO);
settings.setVirtualJoystickDefaultLayout(AppSettings.VIRTUAL_JOYSTICK_LAYOUT_DYNAMIC);
In auto mode it appears on mobile when joystick mappings exist and no hardware gamepad is connected. VIRTUAL_JOYSTICK_ENABLED forces it on, while VIRTUAL_JOYSTICK_DISABLED turns it off.
The default dynamic layout shows controls bound to game actions; select VIRTUAL_JOYSTICK_LAYOUT_XBOX for a fixed Xbox-style layout. Try jme3test.input.TestJoystick on a device or simulator to inspect the controls and mappings.
For a custom layout, find the VirtualJoystick among the initialized joysticks and call setLayout with your own VirtualJoystickLayout subclass. Its protected layout methods let you add and position controls and change their size; coordinates are normalized to the screen.
Use setTheme with a VirtualJoystickTheme to customize textures and fonts. For example, this layout makes the existing Xbox-style controls larger:
import com.jme3.input.Joystick;
import com.jme3.input.virtual.VirtualJoystick;
import com.jme3.input.virtual.VirtualJoystickXboxLayout;
class LargerGamepadLayout extends VirtualJoystickXboxLayout {
LargerGamepadLayout() {
setScale(1.4f);
}
}
// After input initialization, for example in simpleInitApp():
Joystick[] devices = inputManager.getJoysticks();
if (devices != null) {
for (Joystick device : devices) {
if (device instanceof VirtualJoystick) {
((VirtualJoystick) device).setLayout(new LargerGamepadLayout());
}
}
}
A virtual joystick may not be present on every backend or configuration, so check the device type before casting. Test layout changes at different screen sizes and orientations.
Buttons and vibration¶
An action mapping can combine a gamepad button with other input. This example binds the first enumerated button to Jump; in a real game, let the player choose a binding and check the controller's logical button IDs rather than assuming an index is universal.
Joystick[] devices = inputManager.getJoysticks();
if (devices != null && devices.length > 0) {
Joystick pad = devices[0];
if (!pad.getButtons().isEmpty()) {
pad.getButtons().get(0).assignButton("Jump");
inputManager.addListener((ActionListener) (name, pressed, tpf) -> {
if (pressed) {
// Trigger the jump in your game logic.
pad.rumble(0.5f, 0.25f, 0.15f);
}
}, "Jump");
}
}
The two amplitudes represent high- and low-frequency motors; duration is measured in seconds. Use pad.stopRumble() to stop a running effect.
Controller motors and haptic support differ across devices and backends. Test on your target devices and keep game actions usable without vibration.
Virtual controls use the same mapping concepts as physical devices. Try the layout and touch targets in an Android emulator or iOS Simulator, then confirm the experience on real devices.