Skip to main content

Gamepad

caution

This service is rc and may change in the future.

A two axis directional gamepad with optional buttons.

import { Gamepad } from "@devicescript/core"

const gamepad = new Gamepad()

Registers

axes

An array representing the controls with axes present on the device (e.g. analog thumb sticks), as [x, y]. Each entry in the array is a floating point value in the range -1.0 – 1.0``, representing the axis position from the lowest value (-1.0) to the highest value (1.0`).

  • type: ClientRegister<{ x: number; y: number }> (packing format i1.15 i1.15)
import { Gamepad } from "@devicescript/core"

const gamepad = new Gamepad()
// ...
gamepad.axes.subscribe(async ({ x, y }) => {
console.log({ x, y })
})

button

A client register for the requested button or combination of buttons. The value is true if the button is pressed, false otherwise.

  • type: ClientRegister<GamepadButtons>
import { Gamepad, GamepadButtons } from "@devicescript/core"

const gamepad = new Gamepad()
// ...
gamepad.button(GamepadButtons.Down).subscribe(async pressed => {
console.log({ pressed })
})

reading

If the gamepad is analog, the directional buttons should be "simulated", based on gamepad position (Left is { x = -1, y = 0 }, Up is { x = 0, y = -1}). If the gamepad is digital, then each direction will read as either -1, 0, or 1 (in fixed representation). The primary button on the gamepad is A.

  • type: Register<any[]> (packing format u32 i1.15 i1.15)

  • track incoming values

import { Gamepad } from "@devicescript/core"

const gamepad = new Gamepad()
// ...
gamepad.reading.subscribe(async (value) => {
...
})
note

write and read will block until a server is bound to the client.

variant

The type of physical gamepad.

  • type: Register<number> (packing format u8)

  • optional: this register may not be implemented

  • constant: the register value will not change (until the next reset)

  • read only

import { Gamepad } from "@devicescript/core"

const gamepad = new Gamepad()
// ...
const value = await gamepad.variant.read()
note

write and read will block until a server is bound to the client.

buttonsAvailable

Indicates a bitmask of the buttons that are mounted on the gamepad. If the Left/Up/Right/Down buttons are marked as available here, the gamepad is digital. Even when marked as not available, they will still be simulated based on the analog gamepad.

  • type: Register<number> (packing format u32)

  • constant: the register value will not change (until the next reset)

  • read only

import { Gamepad } from "@devicescript/core"

const gamepad = new Gamepad()
// ...
const value = await gamepad.buttonsAvailable.read()
note

write and read will block until a server is bound to the client.

Events

change

Emitted whenever the state of buttons changes.

gamepad.change.subscribe(() => {

})