Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class Gamepad

  1. Object
  2. Gamepad

One connected game controller.

Buttons and axes use the standard layout every modern controller is mapped to – the Xbox layout, which is also what Steam Input presents for a Steam Deck, a Steam Controller or a PlayStation pad. Query it with #isButtonDown(int) and #getAxis(int), or be told about changes through a GamepadListener registered with Gamepads.

Instances are owned by Gamepads and live for as long as the controller is connected; #isConnected() turns false once it is gone. All state is written and read on the EDT.

Fields

public static final int BUTTON_A = 0The bottom face button (A on Xbox, Cross on PlayStation).
public static final int BUTTON_B = 1The right face button (B on Xbox, Circle on PlayStation).
public static final int BUTTON_X = 2The left face button (X on Xbox, Square on PlayStation).
public static final int BUTTON_Y = 3The top face button (Y on Xbox, Triangle on PlayStation).
public static final int BUTTON_LEFT_SHOULDER = 4The left shoulder button.
public static final int BUTTON_RIGHT_SHOULDER = 5The right shoulder button.
public static final int BUTTON_BACK = 6The back / view / select button.
public static final int BUTTON_START = 7The start / menu button.
public static final int BUTTON_GUIDE = 8The guide / home button.
public static final int BUTTON_LEFT_STICK = 9Pressing the left stick.
public static final int BUTTON_RIGHT_STICK = 10Pressing the right stick.
public static final int BUTTON_DPAD_UP = 11D-pad up.
public static final int BUTTON_DPAD_DOWN = 12D-pad down.
public static final int BUTTON_DPAD_LEFT = 13D-pad left.
public static final int BUTTON_DPAD_RIGHT = 14D-pad right.
public static final int BUTTON_LEFT_TRIGGER = 15The left trigger, reported as a button once pulled past halfway.
public static final int BUTTON_RIGHT_TRIGGER = 16The right trigger, reported as a button once pulled past halfway.
public static final int BUTTON_COUNT = 17The number of buttons.
public static final int AXIS_LEFT_X = 0The left stick, -1 (left) to 1 (right).
public static final int AXIS_LEFT_Y = 1The left stick, -1 (up) to 1 (down).
public static final int AXIS_RIGHT_X = 2The right stick, -1 (left) to 1 (right).
public static final int AXIS_RIGHT_Y = 3The right stick, -1 (up) to 1 (down).
public static final int AXIS_LEFT_TRIGGER = 4The left trigger, 0 (released) to 1 (fully pulled).
public static final int AXIS_RIGHT_TRIGGER = 5The right trigger, 0 (released) to 1 (fully pulled).
public static final int AXIS_COUNT = 6The number of axes.

Methods

public int getIndex()The slot this controller occupies, 0 for the first one connected.
public String getName()The name the device reports, for display only; it may be empty.
public boolean isConnected()False once the controller has been disconnected.
public boolean isButtonDown(int button)True while the button is held.
public float getAxis(int axis)The axis value with the dead zone applied: a stick inside the dead zone reads exactly 0, and the remaining travel is rescaled so the value still reaches 1 at the edge.
public float getRawAxis(int axis)The axis value exactly as the device reported it, with no dead zone.
public float getDeadZone()The stick dead zone, 0.15 by default.
public void setDeadZone(float deadZone)Sets the stick dead zone, clamped to 0 .. 0.9.
public void rumble(float strong, float weak, int durationMillis)Rumbles the controller, on ports and controllers that support it; it is silently ignored everywhere else.

Inherited methods

Field details

BUTTON_A

public static final int BUTTON_A = 0
The bottom face button (A on Xbox, Cross on PlayStation).

BUTTON_B

public static final int BUTTON_B = 1
The right face button (B on Xbox, Circle on PlayStation).

BUTTON_X

public static final int BUTTON_X = 2
The left face button (X on Xbox, Square on PlayStation).

BUTTON_Y

public static final int BUTTON_Y = 3
The top face button (Y on Xbox, Triangle on PlayStation).

BUTTON_LEFT_SHOULDER

public static final int BUTTON_LEFT_SHOULDER = 4
The left shoulder button.

BUTTON_RIGHT_SHOULDER

public static final int BUTTON_RIGHT_SHOULDER = 5
The right shoulder button.

BUTTON_BACK

public static final int BUTTON_BACK = 6
The back / view / select button.

BUTTON_START

public static final int BUTTON_START = 7
The start / menu button.

BUTTON_GUIDE

public static final int BUTTON_GUIDE = 8
The guide / home button. Steam keeps this one for itself while a game runs under it, so do not rely on receiving it.

BUTTON_LEFT_STICK

public static final int BUTTON_LEFT_STICK = 9
Pressing the left stick.

BUTTON_RIGHT_STICK

public static final int BUTTON_RIGHT_STICK = 10
Pressing the right stick.

BUTTON_DPAD_UP

public static final int BUTTON_DPAD_UP = 11
D-pad up.

BUTTON_DPAD_DOWN

public static final int BUTTON_DPAD_DOWN = 12
D-pad down.

BUTTON_DPAD_LEFT

public static final int BUTTON_DPAD_LEFT = 13
D-pad left.

BUTTON_DPAD_RIGHT

public static final int BUTTON_DPAD_RIGHT = 14
D-pad right.

BUTTON_LEFT_TRIGGER

public static final int BUTTON_LEFT_TRIGGER = 15
The left trigger, reported as a button once pulled past halfway.

BUTTON_RIGHT_TRIGGER

public static final int BUTTON_RIGHT_TRIGGER = 16
The right trigger, reported as a button once pulled past halfway.

BUTTON_COUNT

public static final int BUTTON_COUNT = 17
The number of buttons.

AXIS_LEFT_X

public static final int AXIS_LEFT_X = 0
The left stick, -1 (left) to 1 (right).

AXIS_LEFT_Y

public static final int AXIS_LEFT_Y = 1
The left stick, -1 (up) to 1 (down).

AXIS_RIGHT_X

public static final int AXIS_RIGHT_X = 2
The right stick, -1 (left) to 1 (right).

AXIS_RIGHT_Y

public static final int AXIS_RIGHT_Y = 3
The right stick, -1 (up) to 1 (down).

AXIS_LEFT_TRIGGER

public static final int AXIS_LEFT_TRIGGER = 4
The left trigger, 0 (released) to 1 (fully pulled).

AXIS_RIGHT_TRIGGER

public static final int AXIS_RIGHT_TRIGGER = 5
The right trigger, 0 (released) to 1 (fully pulled).

AXIS_COUNT

public static final int AXIS_COUNT = 6
The number of axes.

Method details

getIndex

public int getIndex()
The slot this controller occupies, 0 for the first one connected. A slot freed by a disconnect is reused by the next controller.

getName

public String getName()
The name the device reports, for display only; it may be empty.

isConnected

public boolean isConnected()
False once the controller has been disconnected.

isButtonDown

public boolean isButtonDown(int button)
True while the button is held.

Parameters

button int
one of the BUTTON_* constants

getAxis

public float getAxis(int axis)
The axis value with the dead zone applied: a stick inside the dead zone reads exactly 0, and the remaining travel is rescaled so the value still reaches 1 at the edge. Triggers are not subject to the dead zone.

Parameters

axis int
one of the AXIS_* constants

getRawAxis

public float getRawAxis(int axis)
The axis value exactly as the device reported it, with no dead zone.

getDeadZone

public float getDeadZone()
The stick dead zone, 0.15 by default.

setDeadZone

public void setDeadZone(float deadZone)
Sets the stick dead zone, clamped to 0 .. 0.9. Worn sticks rest a little off center; the dead zone stops that from reading as movement.

rumble

public void rumble(float strong, float weak, int durationMillis)
Rumbles the controller, on ports and controllers that support it; it is silently ignored everywhere else.

Parameters

strong float
the low frequency (heavy) motor, 0 to 1
weak float
the high frequency (light) motor, 0 to 1
durationMillis int
how long the rumble lasts