W3cubDocs

/Web APIs

Navigator: getGamepads() method

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The Navigator.getGamepads() method returns an array of 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

getGamepads()

Parameters

None.

Return value

An Array of Gamepad objects, eventually empty.

Exceptions

SecurityError DOMException

Use of this feature was blocked by a Permissions Policy.

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
getGamepads 3521 12 29 No 2215 10.1 37≤37 3525 32 2214 10.3 3.01.5
secure_context_required 86 86 91 No 72 No No 86 No No No No

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getGamepads