Navigator: getGamepads()-Methode

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

* Some parts of this feature may have varying levels of support.

Sicherer Kontext: Diese Funktion ist nur in sicheren Kontexten (HTTPS) in einigen oder allen unterstützenden Browsern verfügbar.

Die Navigator.getGamepads()-Methode gibt ein Array von Gamepad-Objekten zurück, eines für jedes mit dem Gerät verbundene Gamepad.

Elemente im Array können null sein, wenn ein Gamepad während einer Sitzung getrennt wird, damit die verbleibenden Gamepads denselben Index beibehalten.

Syntax

js
getGamepads()

Parameter

Keine.

Rückgabewert

Ein Array von Gamepad-Objekten, eventuell leer.

Ausnahmen

SecurityError DOMException

Die Nutzung dieser Funktion wurde durch eine Permissions Policy blockiert.

Beispiele

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.`,
  );
});

Spezifikationen

Specification
Gamepad
# dom-navigator-getgamepads

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
getGamepads
Secure context required
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Has more compatibility info.

Siehe auch