Wrapper for the Gamepad API that smooths out browser inconsistancies. Exposes changes to gamepads, buttons and axes as events.

Constructors

Properties

_getGamepads: (() => (null | Gamepad)[])
_requestAnimationFrame: ((callback: FrameRequestCallback) => number)
axisDeadZone: number
buttonConfigs: wrapperButtonConfig[]
currentStateOfGamepads: (undefined | EGamepad | Gamepad)[]
gamepadAxisChangeListeners: AxisChangeCallback[]
gamepadButtonChangeListeners: ButtonChangeCallback[]
gamepadConnectListeners: GamepadEventCallback[]
gamepadDisconnectListeners: GamepadEventCallback[]
updateDelay: number

Methods

  • (destructor) - Cleans up any event listeners and stops the gamepad check loop. Do not re-use class instance after calling cleanup().

    Returns void

  • gamepadApiSupported: returns true if the native gamepad api is supported by the browser context

    Returns boolean

  • Returns the result of navigator.getGamepads() from the last update

    Parameters

    • forceUpdate: boolean = false

      If true, navigator.getGamepads() will be called inmediately before returning, if gamepad changes happened since the last update, this will cause those change events to fire.

    Returns (undefined | EGamepad | Gamepad)[]

    An array of gamepad objects, or an empty array if the gamepad api is not supported

  • returns the value of navigator.getGamepads() in a cross-browser compatible way

    Returns (null | EGamepad | Gamepad)[]

    An array of gamepad objects (including any emulated gamepads if the GamepadEmulator was set up), or an empty array if the gamepad api is not supported or gampad permissions haven't been granted.

  • offGamepadAxisChange: remove an existing event listener for when a gamepad axis changes

    Parameters

    • Callback: AxisChangeCallback

      The calback function to remove (must be the same function passed to onGamepadAxisChange())

    Returns void

  • offGamepadButtonChange: remove an existing event listener for when a gamepad button changes

    Parameters

    • Callback: ButtonChangeCallback

      The calback function to remove (must be the same function passed to onGamepadButtonChange())

    Returns void

  • remove an existing event listener for when a gamepad (either real or emulated) is connected

    Parameters

    • Callback: GamepadEventCallback

      The calback function to remove (must be the same function passed to onGamepadConnect())

    Returns void

  • remove an existing event listener for when a gamepad (either real or emulated) is disconnected

    Parameters

    • Callback: GamepadEventCallback

      The calback function to remove (must be the same function passed to onGamepadDisconnect())

    Returns void

  • add an event listener for each time a gamepad axis changes. The callback function will be called with the gamepad index, the gamepad object, and a boolean array of the changed axes, The callback is called separately for each gamepad where axes have changed.

    Parameters

    • Callback: AxisChangeCallback

      The calback function to call when a gamepad axis state changes

    Returns AxisChangeCallback

  • onGamepadButtonChange add an event listener for each time a gamepad button changes. The callback function will be called with the gamepad index, the gamepad object, and a array of the changed buttons containing details about how the button transitioned or false if the button state didn't change this frame. Callback is called separately for each gamepad where buttons have changed.

    Parameters

    Returns ButtonChangeCallback

  • Changes the update delay between browser gamepad api checks (takes effect after the next gamepad update)

    Parameters

    • delay: number

      The new delay between gamepad updates in ms

    Returns void