Gamepad API 的 Gamepad 接口,定义了一个独立的游戏手柄或其他控制器,允许访问控制器的信息,譬如按钮按下的状态、坐标输入的位置。游戏手柄或其他控制器,允许访问如按钮按下,和ID等信息。

Gamepad 对象有两种方式返回值:通过 gamepadconnectedgamepaddisconnected 事件的  gamepad 属性,或者在任意位置抓取 Navigator.getGamepads() 方法返回的数组。

属性

Gamepad.axes 只读
一个表示控制器设备上存在的坐标轴的数组 (比如控制器摇杆)。
Gamepad.buttons 只读
一个 gamepadButton 对象的数组,表示设备上的按键的数组。
Gamepad.connected 只读
一个表示控制器是否仍然连接着系统的布尔值。
Gamepad.displayId 只读
返回与 VRDisplay 相关的 VRDisplay.displayId (如果有相关) — 控制器所控制的 VRDisplay 场景。
Gamepad.id 只读
一个包含着控制器标识信息的 DOMString
Gamepad.index 只读
一个自增的整形数字,对于当前连接到系统的每一个设备是唯一的。
Gamepad.mapping 只读
一个指示浏览器是否被映射到某个已知布局的字符串。
Gamepad.timestamp 只读
一个表示上次控制器数据更新时间的 DOMHighResTimeStamp

Gamepad 的实验性扩展

以下接口被定义于 Gamepad Extensions 规范中,且提供了诸如触觉反馈和WebVR控制器姿态方位信息等实验功能的访问。

Gamepad.hand 只读
一个枚举项,定义了控制器是用哪只手拿着的,或最可能是哪只手拿着的。
Gamepad.hapticActuators 只读
一个包含 GamepadHapticActuator 对象的数组,其中表示控制器上可用的触觉反馈硬件。
Gamepad.pose 只读
一个表示WebVR控制器姿态方位信息的 GamepadPose 对象(比如其在3D 空间中的位置和方向)。 

示例

window.addEventListener("gamepadconnected", function(e) {
  console.log("控制器已连接与 %d 位: %s. %d 个按钮, %d 个坐标方向。",
  e.gamepad.index, e.gamepad.id,
  e.gamepad.buttons.length, e.gamepad.axes.length);
});

规范

规范 状态 备注
Gamepad
Gamepad
Working Draft Initial defintion
WebVR 1.1
displayId
Draft Defines the Gamepad.displayId property.
Gamepad Extensions
Gamepad extensions
Editor's Draft Defines the Experimental extensions to Gamepad

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 35
Full support 35
No support 21 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support YesFirefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari No support NoWebView Android No support NoChrome Android Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 32Opera Android Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari iOS No support NoSamsung Internet Android Full support Yes
Gamepad() constructorChrome Full support 35Edge Full support 12Firefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22Safari No support NoWebView Android No support NoChrome Android Full support 35Edge Mobile Full support YesFirefox Android Full support 32Opera Android Full support 22Safari iOS No support NoSamsung Internet Android Full support Yes
axesChrome Full support 35
Full support 35
No support 21 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support 12Firefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari No support NoWebView Android No support NoChrome Android Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 32Opera Android Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari iOS No support NoSamsung Internet Android Full support Yes
buttonsChrome Full support 35
Full support 35
No support 21 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support 12Firefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari No support NoWebView Android No support NoChrome Android Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 32Opera Android Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari iOS No support NoSamsung Internet Android Full support Yes
connectedChrome Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support 12Firefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari No support NoWebView Android No support NoChrome Android Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 32Opera Android Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari iOS No support NoSamsung Internet Android Full support Yes
displayId
Experimental
Chrome Full support Yes
Notes Disabled
Full support Yes
Notes Disabled
Notes Only works on desktop in an experimental version of Chrome (other builds won't return any devices when Navigator.getVRDisplays() is invoked).
Disabled This feature is behind the WebVR preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 15Firefox Full support 56
Notes
Full support 56
Notes
Notes Currently only Windows support is enabled by default. Mac support is available in Firefox Nightly.
IE No support NoOpera ? Safari ? WebView Android No support NoChrome Android Full support Yes
Notes
Full support Yes
Notes
Notes Currently supported only by Google Daydream.
Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
hand
Experimental
Chrome No support NoEdge Full support 15Firefox Full support Yes
Notes Disabled
Full support Yes
Notes Disabled
Notes The flag is enabled by default in Firefox Nightly and Beta, versions 55 and above.
Disabled This feature is behind the dom.gamepad-extensions.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
hapticActuators
Experimental
Chrome No support NoEdge Full support 15Firefox Full support Yes
Notes Disabled
Full support Yes
Notes Disabled
Notes The flag is enabled by default in Firefox Nightly and Beta, versions 55 and above.
Disabled This feature is behind the dom.gamepad-extensions.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
id
Experimental
Chrome Full support 35
Full support 35
No support 21 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support 12Firefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari No support NoWebView Android No support NoChrome Android Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 32Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
index
Experimental
Chrome Full support 35
Full support 35
No support 21 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support 12Firefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari No support NoWebView Android No support NoChrome Android Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 32Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
mappingChrome Full support 35
Full support 35
No support 21 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support 12Firefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari No support NoWebView Android No support NoChrome Android Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 32Opera Android Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari iOS No support NoSamsung Internet Android Full support Yes
pose
Experimental
Chrome No support NoEdge Full support 15Firefox Full support Yes
Notes Disabled
Full support Yes
Notes Disabled
Notes The flag is enabled by default in Firefox Nightly and Beta, versions 55 and above.
Disabled This feature is behind the dom.gamepad-extensions.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
timestampChrome Full support 35
Full support 35
No support 21 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Full support 12Firefox Full support 29
Full support 29
No support 24 — 28
Disabled
Disabled From version 24 until version 28 (exclusive): this feature is behind the dom.gamepad.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari No support NoWebView Android No support NoChrome Android Full support 35
Full support 35
No support 25 — 34
Prefixed
Prefixed Requires the vendor prefix: webkit
Edge Mobile Full support YesFirefox Android Full support 32Opera Android Full support 22
Full support 22
No support 15 — 21
Prefixed
Prefixed Requires the vendor prefix: webkit
Safari iOS No support NoSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

另请参阅

文档标签和贡献者

此页面的贡献者: zsxeee, lixuanh
最后编辑者: zsxeee,