v152 · input · gamepad

GamepadButton.type

The Gamepad API identifies buttons by position in an array. Seventeen of those positions have agreed meanings; everything past them is a number with no interpretation, which is where the trackpad, the paddles, the touch-sensitive face buttons and the capture key all live. A type on each button gives them names instead.

concepts

  1. What the interfaces expose

    Every member of Gamepad and GamepadButton in this browser, and a live view of anything actually connected — because the shape of the API is easier to argue about than to remember.

  2. The seventeen agreed positions

    The standard mapping, index by index, with a connected controller's live state laid over it. Press something and watch which index it is.

  3. Past the end of the map

    What an application does today with index 17 and beyond: guess from the device id, ask the user, or ignore it. Build the lookup table the hard way, then see what a type replaces.

why it shipped

The standard mapping is a good agreement about a small set of controls: two sticks, a d-pad, four face buttons, four shoulder controls, and three system buttons. It says nothing about anything else, and every controller made in the last decade has something else — a trackpad, back paddles, a capture button, a second set of grips.

Applications cope by matching on gamepad.id, which is a free-text string that differs between operating systems and browsers for the same physical device. That produces a lookup table nobody can maintain and which is wrong for any controller released after the table was written. A type on the button moves the identification to where the browser already knows the answer.

the API

for (const [index, button] of gamepad.buttons.entries()) {
  // Today: index is all you have, and only the first 17 mean anything.
  // With the addition: a name, from the browser, for the ones that have one.
  console.log(index, button.pressed, button.value, button.type);
}

enabling it now

Not available on the Chrome 150 used to build these pages, with or without a flag. Measured: "type" in GamepadButton.prototype is false both with and without --enable-experimental-web-platform-features, while the rest of the interface — pressed, touched, value — is present in the same check.

Object.getOwnPropertyNames(GamepadButton.prototype)
// ["pressed", "touched", "value", "constructor"] — no "type"

These pages also need a controller to show anything live. Where none is connected they say so and keep the parts that do not need one.

references