--- 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") }}
35.0

{{CompatVersionUnknown}} {{ CompatGeckoDesktop("29.0") }} [1] {{ CompatNo() }}

15.0 {{ property_prefix("webkit") }}
22.0

{{ CompatNo() }}
Feature Android Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
General support {{ CompatNo() }} {{CompatVersionUnknown}} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

[1] 自 Firefox 24 起可以通过偏好设置启用。

另请参阅