Back to Content

Navigator: getGamepads() method

files/en-us/web/api/navigator/getgamepads/index.md

latest1.1 KB
Original Source

{{APIRef("Gamepad API")}}

The Navigator.getGamepads() method returns an array of {{domxref("Gamepad")}} objects, one for each gamepad connected to the device.

Elements in the array may be null if a gamepad disconnects during a session, so that the remaining gamepads retain the same index.

Syntax

js-nolint
getGamepads()

Parameters

None.

Return value

An {{jsxref("Array")}} of {{domxref("Gamepad")}} objects, eventually empty.

Exceptions

  • SecurityError {{domxref("DOMException")}}

Examples

js
window.addEventListener("gamepadconnected", (e) => {
  const gp = navigator.getGamepads()[e.gamepad.index];
  console.log(
    `Gamepad connected at index ${gp.index}: ${gp.id} with ${gp.buttons.length} buttons, ${gp.axes.length} axes.`,
  );
});

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also