Navigator.getGamepads()

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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.

Calls to this method will throw a SecurityError DOMException if disallowed by the gamepad Feature Policy.

Syntax

 var gamepads = navigator.getGamepads();

Example

window.addEventListener("gamepadconnected", function(e) {
  var gp = navigator.getGamepads()[e.gamepad.index];
  console.log(
    "Gamepad connected at index %d: %s. %d buttons, %d axes.",
    gp.index, gp.id, gp.buttons.length, gp.axes.length
  );
});

Specifications

Specification
Gamepad
# dom-navigator-getgamepads

Browser compatibility

BCD tables only load in the browser

See also