--- title: Navigator.getGamepads() slug: Web/API/Navigator/getGamepads translation_of: Web/API/Navigator/getGamepads ---
{{APIRef("Gamepad API")}}{{SeeCompatTable}}
调用 Navigator.getGamepads()
方法会返回一个数组:第一个值为 null
,其他的值均为 {{ domxref("Gamepad") }} 对象,表示每一个与设备连接的游戏手柄。所以如果没有连接任何游戏手柄,这个方法将只会返回 null
。
var arrayGP = navigator.getGamepads();
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); });
Specification | Status | Comment |
---|---|---|
{{SpecName('Gamepad', '', 'The Gamepad API specification')}} | {{Spec2('Gamepad')}} | 初始定义 |
{{ CompatibilityTable() }}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
General support |
21.0 {{ property_prefix("webkit") }} |
{{CompatVersionUnknown}} | {{ CompatGeckoDesktop("29.0") }} [1] | {{ CompatNo() }} |
15.0 {{ property_prefix("webkit") }} |
{{ CompatNo() }} |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
General support | {{ CompatNo() }} | {{CompatVersionUnknown}} | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} |
[1] 自 Firefox 24 起可以通过偏好设置启用。