Class Gamepad

java.lang.Object
com.codename1.gaming.Gamepad

public final class Gamepad extends Object

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.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The number of axes.
    static final int
    The left trigger, 0 (released) to 1 (fully pulled).
    static final int
    The left stick, -1 (left) to 1 (right).
    static final int
    The left stick, -1 (up) to 1 (down).
    static final int
    The right trigger, 0 (released) to 1 (fully pulled).
    static final int
    The right stick, -1 (left) to 1 (right).
    static final int
    The right stick, -1 (up) to 1 (down).
    static final int
    The bottom face button (A on Xbox, Cross on PlayStation).
    static final int
    The right face button (B on Xbox, Circle on PlayStation).
    static final int
    The back / view / select button.
    static final int
    The number of buttons.
    static final int
    D-pad down.
    static final int
    D-pad left.
    static final int
    D-pad right.
    static final int
    D-pad up.
    static final int
    The guide / home button.
    static final int
    The left shoulder button.
    static final int
    Pressing the left stick.
    static final int
    The left trigger, reported as a button once pulled past halfway.
    static final int
    The right shoulder button.
    static final int
    Pressing the right stick.
    static final int
    The right trigger, reported as a button once pulled past halfway.
    static final int
    The start / menu button.
    static final int
    The left face button (X on Xbox, Square on PlayStation).
    static final int
    The top face button (Y on Xbox, Triangle on PlayStation).
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    float
    The stick dead zone, 0.15 by default.
    int
    The slot this controller occupies, 0 for the first one connected.
    The name the device reports, for display only; it may be empty.
    float
    getRawAxis(int axis)
    The axis value exactly as the device reported it, with no dead zone.
    boolean
    isButtonDown(int button)
    True while the button is held.
    boolean
    False once the controller has been disconnected.
    void
    rumble(float strong, float weak, int durationMillis)
    Rumbles the controller, on ports and controllers that support it; it is silently ignored everywhere else.
    void
    setDeadZone(float deadZone)
    Sets the stick dead zone, clamped to 0 ..

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BUTTON_A

      public static final int BUTTON_A
      The bottom face button (A on Xbox, Cross on PlayStation).
      See Also:
    • BUTTON_B

      public static final int BUTTON_B
      The right face button (B on Xbox, Circle on PlayStation).
      See Also:
    • BUTTON_X

      public static final int BUTTON_X
      The left face button (X on Xbox, Square on PlayStation).
      See Also:
    • BUTTON_Y

      public static final int BUTTON_Y
      The top face button (Y on Xbox, Triangle on PlayStation).
      See Also:
    • BUTTON_LEFT_SHOULDER

      public static final int BUTTON_LEFT_SHOULDER
      The left shoulder button.
      See Also:
    • BUTTON_RIGHT_SHOULDER

      public static final int BUTTON_RIGHT_SHOULDER
      The right shoulder button.
      See Also:
    • BUTTON_BACK

      public static final int BUTTON_BACK
      The back / view / select button.
      See Also:
    • BUTTON_START

      public static final int BUTTON_START
      The start / menu button.
      See Also:
    • BUTTON_GUIDE

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

      public static final int BUTTON_LEFT_STICK
      Pressing the left stick.
      See Also:
    • BUTTON_RIGHT_STICK

      public static final int BUTTON_RIGHT_STICK
      Pressing the right stick.
      See Also:
    • BUTTON_DPAD_UP

      public static final int BUTTON_DPAD_UP
      D-pad up.
      See Also:
    • BUTTON_DPAD_DOWN

      public static final int BUTTON_DPAD_DOWN
      D-pad down.
      See Also:
    • BUTTON_DPAD_LEFT

      public static final int BUTTON_DPAD_LEFT
      D-pad left.
      See Also:
    • BUTTON_DPAD_RIGHT

      public static final int BUTTON_DPAD_RIGHT
      D-pad right.
      See Also:
    • BUTTON_LEFT_TRIGGER

      public static final int BUTTON_LEFT_TRIGGER
      The left trigger, reported as a button once pulled past halfway.
      See Also:
    • BUTTON_RIGHT_TRIGGER

      public static final int BUTTON_RIGHT_TRIGGER
      The right trigger, reported as a button once pulled past halfway.
      See Also:
    • BUTTON_COUNT

      public static final int BUTTON_COUNT
      The number of buttons.
      See Also:
    • AXIS_LEFT_X

      public static final int AXIS_LEFT_X
      The left stick, -1 (left) to 1 (right).
      See Also:
    • AXIS_LEFT_Y

      public static final int AXIS_LEFT_Y
      The left stick, -1 (up) to 1 (down).
      See Also:
    • AXIS_RIGHT_X

      public static final int AXIS_RIGHT_X
      The right stick, -1 (left) to 1 (right).
      See Also:
    • AXIS_RIGHT_Y

      public static final int AXIS_RIGHT_Y
      The right stick, -1 (up) to 1 (down).
      See Also:
    • AXIS_LEFT_TRIGGER

      public static final int AXIS_LEFT_TRIGGER
      The left trigger, 0 (released) to 1 (fully pulled).
      See Also:
    • AXIS_RIGHT_TRIGGER

      public static final int AXIS_RIGHT_TRIGGER
      The right trigger, 0 (released) to 1 (fully pulled).
      See Also:
    • AXIS_COUNT

      public static final int AXIS_COUNT
      The number of axes.
      See Also:
  • 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: 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: 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: the low frequency (heavy) motor, 0 to 1

      • weak: the high frequency (light) motor, 0 to 1

      • durationMillis: how long the rumble lasts