Class Gamepads

java.lang.Object
com.codename1.gaming.Gamepads

public final class Gamepads extends Object

The connected game controllers.

Ports that read controllers report them here; applications ask for them with #getGamepads() / #getGamepad(int) and listen with #addListener(GamepadListener). Where no port reports any controller the list is simply empty.

Focus navigation. Unless turned off with #setFocusNavigation(boolean), a button no listener consumed drives the user interface the way the keyboard does: the D-pad and the left stick move focus, A presses the focused component and B goes back. An ordinary form is therefore usable from a couch with no code written for it.

Everything here runs on the EDT. Ports call the report* methods from it.

  • Method Details

    • getGamepads

      public static Gamepad[] getGamepads()
      The connected controllers, in slot order. A new array every call.
    • getGamepad

      public static Gamepad getGamepad(int index)
      The controller in the given slot, or null when that slot is empty.
    • isAnyConnected

      public static boolean isAnyConnected()
      True when at least one controller is connected.
    • addListener

      public static void addListener(GamepadListener l)
      Registers a listener.
    • removeListener

      public static void removeListener(GamepadListener l)
      Unregisters a listener.
    • isFocusNavigation

      public static boolean isFocusNavigation()
      Whether unconsumed buttons drive focus navigation; on by default.
    • setFocusNavigation

      public static void setFocusNavigation(boolean b)
      Turns focus navigation from controller buttons on or off. A game that reads the controller itself and has no focusable components may turn it off.
    • reportConnected

      public static Gamepad reportConnected(int index, String name)
      Called by a port when a controller connects; index is the slot it chose. Returns the controller object.
    • reportDisconnected

      public static void reportDisconnected(int index)
      Called by a port when a controller disconnects. Releases every button it still held first, so nothing is left stuck down.
    • reportButton

      public static void reportButton(int index, int button, boolean down)
      Called by a port when a button changes.
    • reportButton

      public static void reportButton(int index, int button, boolean down, boolean navigate)
      Called by a port when a button changes. A port that already delivers the controller to the user interface as keys (the Xbox build of the Windows port does) passes navigate false, so the press is not delivered twice.
    • reportAxis

      public static void reportAxis(int index, int axis, float value)
      Called by a port when an axis changes: sticks -1 .. 1, triggers 0 .. 1. The triggers are also reported as Gamepad#BUTTON_LEFT_TRIGGER and Gamepad#BUTTON_RIGHT_TRIGGER past halfway, and the left stick as the D-pad for focus navigation.