Class GamePad

java.lang.Object
  extended by GamePad

public class GamePad
extends java.lang.Object

A high-level wrapper to a controller. This is a singleton class that returns all of the controllers found connected to this machine.

Note that this is designed to work with DualShock Playstation-like controllers (but without rumble).

These are controllers with 4 action buttons, 6 L and R buttons (3 for left and 3 for right), 2 middle buttons (select and start), a d-pad (the direction buttons) and 2 analogue sticks.

Author:
Joseph Lenton

Nested Class Summary
static class GamePad.Axis
          The different types of axis on the joypad.
static class GamePad.Button
          An enum of all of the different buttons most commonly supported.
 
Method Summary
 Direction getAxis(GamePad.Axis axis)
          This returns a Direction describing the current angle and strength of the Axis polled.
static GamePad getGamePad()
          This returns the first GamePad that this library finds connected to your PC.
static GamePad getGamePad(int index)
          For grabbing a specific gamepad that is connected to your PC.
static GamePad[] getGamePads()
          Returns all of the GamePads that this library can find connected to your PC.
static int getNumGamePads()
           
 boolean isAxisSupported(GamePad.Axis axis)
          Checks if the given axis is supported or not.
 boolean isButtonSupported(GamePad.Button button)
          Checks if the given button is supported on your gamepad, and so will work when pressed, or not.
 boolean isDown(GamePad.Button button)
          Checks if the button given is pressed down or not.
static void refreshGamePads()
          Rescans the PC for all GamePads connected.
 void runConfigurePad()
          Runs and shows a GamePad configuration GUI for setting gamepad mappings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAxis

public Direction getAxis(GamePad.Axis axis)

This returns a Direction describing the current angle and strength of the Axis polled. This will return a Direction regardless of if the Axis is in use or not.

Axis which are not supported still return a Direction. But it will just never show any movement.

To tell if the Direction is or is not in use then you should check the strength of the Direction.

Parameters:
axis - The Axis to poll.
Returns:
The current Direction of the Axis polled.

getGamePad

public static GamePad getGamePad()

This returns the first GamePad that this library finds connected to your PC. If there is only one GamePad connected, then that is the one that is returned.

If no game pad is plugged in then this will throw an exception. This will always return the same first GamePad it has found.

Returns:
The first GamePad found.
Throws:
java.lang.IllegalStateException - if there is no real game pad found.

getGamePad

public static GamePad getGamePad(int index)

For grabbing a specific gamepad that is connected to your PC.

The index given should be from 0 to the number of game pads on this machine, exclusive.

Parameters:
index - The index of the GamePad to find.
Returns:
The GamePad stored against the given index.

getGamePads

public static GamePad[] getGamePads()

Returns all of the GamePads that this library can find connected to your PC.

If no pads are found then an empty array is returned.

Returns:
An array containing all of the GamePads found connected to this machine.

getNumGamePads

public static int getNumGamePads()
Returns:
The number of game pads available on this PC, 0 for no pads.

isAxisSupported

public boolean isAxisSupported(GamePad.Axis axis)

Checks if the given axis is supported or not.

Parameters:
The - Axis object to check.
Returns:
True if the given axis is available to be read on this GamePad.

isButtonSupported

public boolean isButtonSupported(GamePad.Button button)

Checks if the given button is supported on your gamepad, and so will work when pressed, or not.

Returns:
True if the given button enum is supported by this controller.

isDown

public boolean isDown(GamePad.Button button)

Checks if the button given is pressed down or not. If the button is not supported then this will always fail silently and return false.

Parameters:
button - The button to check.
Returns:
True if the button with the given index is pressed, otherwise false.

refreshGamePads

public static void refreshGamePads()

Rescans the PC for all GamePads connected. Calling getGamePads() after this will return the latest number of pads connected to the machine.


runConfigurePad

public void runConfigurePad()

Runs and shows a GamePad configuration GUI for setting gamepad mappings.

This is what you can use for allowing users to reconfigure their gamepad within your scenario.