Class Controller

Constructors

  • Creates a new controller instance and passes the gamepad it will be created with

    Parameters

    • pGamepad: Gamepad

      A gamepad object

    Returns Controller

Properties

config: {
    buttons: {
        [key: string]: number;
    };
} = ...

Configuration of which buttons / analogs map to which indexes

Type declaration

  • buttons: {
        [key: string]: number;
    }
    • [key: string]: number
leftAnalogHeld: boolean = false

Whether the left analog is currently being held

rightAnalogHeld: boolean = false

Whether the right analog is currently being held

baseAnalogPos: {
    x: number;
    y: number;
} = ...

The base analogs position when it is not in use

Type declaration

  • x: number
  • y: number
AXES: {
    [key: string]: number;
} = ...

Analog thumb sticks

Type declaration

  • [key: string]: number
AXES_REVERSED_MAP: {
    [key: number]: string;
} = ...

Type declaration

  • [key: number]: string
AXIS_UPDATE_RANGE: number = 0.0

The range at which axis changes are detected

ANALOG_RELEASE_RANGE: number = 0.09

The range at which the analog is considered to be dropped -0.09 - 0.09

TRIGGER_PRESSED_VALUE: number = 0.12

The value at which holding a trigger (LT OR RT) will consider it being pressed

PRESSED: number = 1.0

Value to indicate a pressed button

UNPRESSED: number = 0.0

Value to indicate a button is not pressed

BUTTONS_MAP: {
    [key: string]: number;
} = ...

A button map that maps common button names to the indexes the computer knows them as

Type declaration

  • [key: string]: number
BUTTONS_REVERSE_MAP: {
    [key: number]: string;
} = ...

A reverse map of the button names

Type declaration

  • [key: number]: string
PS4_REMAPPED: {
    [key: string]: string;
} = ...

A small remapped version of the controllers button_map with PS4 alternatives

Type declaration

  • [key: string]: string
GAMEPAD_IDS: {
    [key: string]: string;
} = ...

PS: Playstation vendor Xbox: XBOX vendor PC: Computer PC vendor Android: Android device vendor

Type declaration

  • [key: string]: string
pressed: {
    [key: string]: boolean;
} = ...

Object full of the currently held down buttons

Type declaration

  • [key: string]: boolean
info: {
    axes: null | readonly number[];
    buttons: null | readonly GamepadButton[];
    previousButtonState: boolean[];
    previousAxesState: (undefined | number)[];
    initialAxesStickDrift: (undefined | number)[];
} = ...

Info about the controller

Type declaration

  • axes: null | readonly number[]
  • buttons: null | readonly GamepadButton[]
  • previousButtonState: boolean[]
  • previousAxesState: (undefined | number)[]
  • initialAxesStickDrift: (undefined | number)[]
leftAnalogPos: {
    x: number;
    y: number;
} = ...

The left analogs position

Type declaration

  • x: number
  • y: number
rightAnalogPos: {
    x: number;
    y: number;
} = ...

The right analogs position

Type declaration

  • x: number
  • y: number
pressHandlers: {
    [key: string]: ((buttonName, value, repeat) => void) | null;
} = {}

Object of stored callback that will call when a button is pressed

Type declaration

  • [key: string]: ((buttonName, value, repeat) => void) | null
releaseHandlers: {
    [key: string]: ((buttonName, value) => void) | null;
} = {}

Object of stored callback that will call when a button is released

Type declaration

  • [key: string]: ((buttonName, value) => void) | null
axisHandlers: {
    [key: string]: ((analog) => void) | ((axisName, value, angle, repeat) => void) | null;
} = {}

Object of stored callbacks that will call when the axis is changed

Type declaration

  • [key: string]: ((analog) => void) | ((axisName, value, angle, repeat) => void) | null
timestamp: number = 0

The timestamp of the gamepad.

index: number = 0

The index of the controller.

gamepad: Gamepad

The gamepad object

type: string

The type of controller

Methods

  • Returns the type the controller is. PC / PS / Xbox / Android

    Returns string

    The controller type

  • Update the state of this controller with the latest polled information

    Parameters

    • pGamepad: Gamepad

      The gamepad with the new updated state

    Returns void

  • Gets the current buttons pressed down on the gamepad.

    Returns string[]

  • Attaches a callback to the specified event.

    Parameters

    • pEvent: string

      The event to attach the callback to

    • pCallback: ((buttonName, value, repeat) => void) | ((buttonName, value) => void) | ((analog) => void) | ((axisName, value, angle, repeat) => void)

      The function to be called when the event is triggered

    Returns Controller

    The Controller instance

  • Removes a callback from the specified event.

    Parameters

    • pEvent: string

      The event to remove the callback from

    Returns Controller

    The Controller instance

  • Handles the input on the buttons.

    Parameters

    • pButton: number

      The button index that was pressed

    • pValue: number

      The value of the button (0 for unpressed, 1 for pressed) 0-1 for buttons that have a range

    • pRepeat: boolean

      Whether this button is still being held from a previous frame

    • pPressed: boolean

      Whether this button is being pressed in this current frame.

    Returns void

  • Handles the input on the analogs.

    Parameters

    • pAxis: number

      The axis index that was moved

    • pValue: number

      The value of the axis that was moved (0-1 range)

    • pRepeat: boolean

      Whether this axes is still the same from a previous frame

    Returns void

  • Handles the event for when a analog is grabbed.

    Parameters

    • pAnalog: string

      Analog that was grabbed

    Returns void

  • Handles the event for when a analog is dropped.

    Parameters

    • pAnalog: string

      Analog that was dropped

    Returns void

  • Whether the left analog is being held

    Returns boolean

    True if left analog is held

  • Whether the right analog is being held

    Returns boolean

    True if right analog is held

  • Checks whether a button is pressed down or not

    Parameters

    • pButtonName: string

      The button to check if its pressed

    Returns boolean

    True if button is pressed

  • Vibrate the controller (experimental)

    dual-rumble: Dual-rumble describes a haptic configuration with an eccentric rotating mass vibration motor in each handle of a standard gamepad. In this configuration, either motor is capable of vibrating the whole gamepad. The two masses are unequal so that the effects of each can be combined to create more complex haptic effects.

    Parameters

    • pVibrationType: string = 'dual-rumble'

      The type of rumble. "dual-rumble", or "vibration"

    • pStartDelay: number = 0

      The start delay before the vibration occurs in ms

    • pDuration: number = 1000

      The duration of the vibration in ms

    • pWeakMagnitude: number = 1

      The magnitude of the weak actuator (between 0 and 1).

    • pStrongMagnitude: number = 1

      The magnitude of the strong actuator (between 0 and 1).

    Returns void

  • The pulse() method of the GamepadHapticActuator interface makes the hardware pulse at a certain intensity for a specified duration. (From MDN)

    Parameters

    • pValue: number = 1

      A double representing the intensity of the pulse. This can vary depending on the hardware type, but generally takes a value between 0.0 (no intensity) and 1.0 (full intensity).

    • pDuration: number = 200

      A double representing the duration of the pulse, in milliseconds.

    Returns void