Class Gamepads
java.lang.Object
com.codename1.gaming.Gamepads
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 Summary
Modifier and TypeMethodDescriptionstatic voidRegisters a listener.static GamepadgetGamepad(int index) The controller in the given slot, or null when that slot is empty.static Gamepad[]The connected controllers, in slot order.static booleanTrue when at least one controller is connected.static booleanWhether unconsumed buttons drive focus navigation; on by default.static voidUnregisters a listener.static voidreportAxis(int index, int axis, float value) Called by a port when an axis changes: sticks -1 ..static voidreportButton(int index, int button, boolean down) Called by a port when a button changes.static voidreportButton(int index, int button, boolean down, boolean navigate) Called by a port when a button changes.static GamepadreportConnected(int index, String name) Called by a port when a controller connects;indexis the slot it chose.static voidreportDisconnected(int index) Called by a port when a controller disconnects.static voidsetFocusNavigation(boolean b) Turns focus navigation from controller buttons on or off.
-
Method Details
-
getGamepads
The connected controllers, in slot order. A new array every call. -
getGamepad
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
Registers a listener. -
removeListener
Unregisters a listener. -
reportConnected
-
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) passesnavigatefalse, 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 asGamepad#BUTTON_LEFT_TRIGGERandGamepad#BUTTON_RIGHT_TRIGGERpast halfway, and the left stick as the D-pad for focus navigation.
-