Class Gamepad

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/.

Hierarchy (view full)

Constructors

Properties

axes: number[] = []

Axes values for the gamepad.

buttons: Button[] = []

Gamepad buttons with their associated state.

Should be different for every gamepad.

connected: boolean = false

Indicates if the gamepad is connected to the system.

deadzone: number = 0.05

Deadzone for the gamepad analog sticks.

Values (in absolute) below this threshold are considered 0.

gamepad: any = null

Gamepad API object used to acess the gamepad input.

id: string = null

Identifier of the gamepad device.

index: number = -1

Index of the gamepad instance.

mapping: GamepadMappingType = null
vibrationActuator: GamepadHapticActuator = null

Vibration actuator used to provide haptic feedback.

https://www.w3.org/TR/gamepad/#dom-gamepadhapticactuator

Methods

  • Apply deadzone to the value.

    Parameters

    • value: number

      Value to apply the deadzone to.

    Returns number

    Value with the deadzone applied.

  • Check if a button exists in the connected public static

    Parameters

    • button: number

      Button to check status of

    Returns boolean

    True if button exists in the connected gamepad.

  • Check if a gamepad button was just pressed.

    Parameters

    • button: number

      Button to check status of

    Returns boolean

    True if button was just pressed

  • Check if a gamepad button was just released.

    Parameters

    • button: number

      Button to check status of

    Returns boolean

    True if button was just released

  • Check if gamepad button is currently pressed.

    Parameters

    • button: number

      Button to check status of

    Returns boolean

    True if button is currently pressed

  • Disconnect the gamepad, reset connection flags.

    When a gamepad is temporarly disconnected this method should be called to reset the connection flags.

    Returns void

  • 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

    • buttonIndex: number

      Button to get analogue value from.

    Returns number

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

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

    Parameters

    • axisIndex: number

      Index of the axis to get value from.

    Returns number

    Value between -1 and 1 depending on the axis direction

  • Get gamepads connected to the system.

    Returns any[]

    Array with all gamepads connected to the system.

  • Reset the haptic actuator of the gamepad. Stops all effects.

    Returns void

  • 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

    • gamepad: any

      Browser gamepad object.

    Returns any

  • Get vendor id and product id for the connected gamepad.

    Parameters

    • gamepad: any

      Gamepad object.

    Returns void

  • Start a haptic effect on the gamepad.

    Parameters

    • type: GamepadHapticEffectType

      Type of the effect to start.

    • params: GamepadEffectParameters

      Parameters for the effect.

    Returns void

  • Update the gamepad state.

    Should be called every frame before checking the buttons values.

    Returns void