web api instance method
Navigator: getGamepads() method
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
getGamepads()
Parameters
None.
Return value
An Array of Gamepad objects, eventually empty.
Exceptions
SecurityErrorDOMException- : Use of this feature was blocked by a Permissions Policy.
Examples
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
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.