Gamepad Class

Module: Input

Gamepad provides basic support for gamepads.

Some gamepads require a button press to being detected.

Gamepad implementation across browsers is still fragmented, every browser implements it a bit differently, so test it on every target before deploying an application using it.

For more information about the Gamepad API state take look at the W3C Gamepad API page https:// www.w3.org/TR/gamepad/.

Methods

buttonExists

(
  • button
)
Boolean

Check if a button exists in the connected Gamepad.

Parameters:

  • button Number

    Button to check status of

Returns:

Boolean:

True if button exists in the connected gamepad.

buttonJustPressed

(
  • button
)
Boolean

Check if a gamepad button was just pressed.

Parameters:

  • button Number

    Button to check status of

Returns:

Boolean:

True if button was just pressed

buttonJustReleased

(
  • button
)
Boolean

Check if a gamepad button was just released.

Parameters:

  • button Number

    Button to check status of

Returns:

Boolean:

True if button was just released

buttonPressed

(
  • button
)
Boolean

Check if gamepad button is currently pressed.

Parameters:

  • button Number

    Button to check status of

Returns:

Boolean:

True if button is currently pressed

disconnect

()

Disconnect this gamepad object.

getAnalogueButton

(
  • button
)
Number

Get analog button value between 0 and 1.

If the button is not analog enabled it will return 0 if button is not pressed or 1 if the button is pressed.

Parameters:

  • button Number

    Button to get analogue value from.

Returns:

Number:

Value between 0 and 1 depending how hard the button is pressed.

getAxis

(
  • Axis
)
Number

Get axis value between -1 and 1 depending on the direction.

Parameters:

  • Axis Number

    to get value from.

Returns:

Number:

Value between -1 and 1 depending on the axis direction

setGamepad

(
  • Browser
)

Set which gamepad should be used by this Gamepad instance.

Can be used to override the gamepad attached to this object and enable multiple gamepad support.

Parameters:

  • Browser Object

    gamepad object.

setProductVendor

(
  • gamepad
)

Get vendor id and product id for the connected gamepad.

Parameters:

  • gamepad Object

    Gamepad object.

update

()

Update the gamepad state.

Should be called every frame before checking the buttons values.

Attributes

A

Number

Gamepad A button.

B

Number

Gamepad B button.

buttons

Array

Gamepad buttons with their associated state.

Should be different for every gamepad.

C

Number

Gamepad C button.

connected

Boolean

Connected state of the gamepad.

D

Number

Gamepad D button.

DOWN

Number

Gamepad DOWN button.

HOME

Number

Gamepad HOME button.

L1

Number

Gamepad L1 button.

L2

Number

Gamepad L2 button.

L3

Number

Gamepad L3 button.

LEFT

Number

Gamepad LEFT button.

LEFT_ANALOGUE_BUT

Number

Gamepad LEFT_ANALOGUE_BUT axis.

LEFT_ANALOGUE_HOR

Number

Gamepad LEFT_ANALOGUE_HOR axis.

LEFT_ANALOGUE_VERT

Number

Gamepad LEFT_ANALOGUE_VERT axis.

LEFT_TRIGGER_A

Number

Gamepad LEFT_TRIGGER_A button.

LEFT_TRIGGER_B

Number

Gamepad LEFT_TRIGGER_B button.

product

Number

Product code of the gamepad device.

R1

Number

Gamepad R1 button.

R2

Number

Gamepad R2 button.

R3

Number

Gamepad R3 button.

RIGHT

Number

Gamepad RIGHT button.

RIGHT_ANALOGUE_BUT

Number

Gamepad RIGHT_ANALOGUE_BUT axis.

RIGHT_ANALOGUE_HOR

Number

Gamepad RIGHT_ANALOGUE_HOR axis.

RIGHT_ANALOGUE_VERT

Number

Gamepad RIGHT_ANALOGUE_VERT axis.

RIGHT_TRIGGER_A

Number

Gamepad RIGHT_TRIGGER_A button.

RIGHT_TRIGGER_B

Number

Gamepad RIGHT_TRIGGER_B button.

SELECT

Number

Gamepad SELECT button.

START

Number

Gamepad START button.

UP

Number

Gamepad UP button.

vendor

Number

Vendor code of the gamepad device.

X

Number

Gamepad X button.

Y

Number

Gamepad Y button.