Gamepad

Gamepad interface of the 手柄 API defines an individual gamepad or other controller, allowing access to information such as button presses, axis positions, and id.

A Gamepad object can be returned in one of two ways: via the gamepad 特性为 gamepadconnected and gamepaddisconnected events, or by grabbing any position in the array returned by the Navigator.getGamepads() 方法。

特性

Gamepad.axes 只读
An array representing the controls with axes present on the device (e.g. analog thumb sticks).
Gamepad.buttons 只读
An array of gamepadButton objects representing the buttons present on the device.
Gamepad.connected 只读
A boolean indicating whether the gamepad is still connected to the system.
Gamepad.displayId 只读
返回 VRDisplay.displayId of an associated VRDisplay (if relevant) — the VRDisplay that the gamepad is controlling the displayed scene of.
Gamepad.id 只读
A DOMString containing identifying information about the controller.
Gamepad.index 只读
An integer that is auto-incremented to be unique for each device currently connected to the system.
Gamepad.mapping 只读
A string indicating whether the browser has remapped the controls on the device to a known layout.
Gamepad.timestamp 只读
A DOMHighResTimeStamp representing the last time the data for this gamepad was updated.

Experimental extensions to Gamepad

The following interfaces are defined in the Gamepad Extensions specification, and provide access to experimental features like haptic feedback and WebVR controller pose information.

Gamepad.hand 只读
An enum defining what hand the controller is being held in, or is most likely to be held in.
Gamepad.hapticActuators 只读
An array containing GamepadHapticActuator objects, each of which represents haptic feedback hardware available on the controller.
Gamepad.pose 只读
A GamepadPose object representing the pose information associated with a WebVR controller (e.g. its position and orientation in 3D space).

范例

window.addEventListener("gamepadconnected", function(e) {
  console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
  e.gamepad.index, e.gamepad.id,
  e.gamepad.buttons.length, e.gamepad.axes.length);
});
					

规范

规范 状态 Comment
Gamepad
The definition of 'Gamepad' in that specification.
工作草案 初始定义
WebVR 1.1
The definition of 'displayId' in that specification.
草案 Defines the Gamepad.displayId 特性。
Gamepad Extensions
The definition of 'Gamepad extensions' in that specification.
编者草案 Defines the Experimental extensions to Gamepad

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Gamepad Chrome 35
35
不支持 21 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge ≤18 Firefox 29
29
不支持 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 Opera 22
22
不支持 15 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 32 Opera Android 22
22
不支持 14 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
不支持 2.0 — 3.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Gamepad() 构造函数 Chrome 35 Edge 12 Firefox 29
29
不支持 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 Opera 22 Safari 10.1 WebView Android No Chrome Android 35 Firefox Android 32 Opera Android 22 Safari iOS 10.3 Samsung Internet Android 4.0
axes Chrome 35
35
不支持 21 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 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 Opera 22
22
不支持 15 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 32 Opera Android 22
22
不支持 14 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
不支持 2.0 — 3.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
buttons Chrome 35
35
不支持 21 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 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 Opera 22
22
不支持 15 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 32 Opera Android 22
22
不支持 14 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
不支持 2.0 — 3.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
connected Chrome 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 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 Opera 22
22
不支持 15 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 32 Opera Android 22
22
不支持 14 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
不支持 2.0 — 3.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
displayId
弃用
Chrome ? — 80
Disabled
不支持 ? — 80
Disabled
Only works on desktop in an experimental version of Chrome (other builds won't return any devices when Navigator.getVRDisplays() is invoked).
Disabled Until version 80 (exclusive): this feature is behind the WebVR preference. To change preferences in Chrome, visit chrome://flags.
Edge 15 Firefox 55
55
Windows support was enabled in Firefox 55.
64
macOS support was enabled in Firefox 64.
IE No Opera ? Safari No WebView Android No Chrome Android ? — 80
不支持 ? — 80
Currently supported only by Google Daydream.
Firefox Android 55 Opera Android ? Safari iOS No Samsung Internet Android Yes
Yes
Currently supported only by Google Daydream.
hand
Chrome No Edge 15 — 79 Firefox Yes
Disabled
Yes
Disabled
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 Opera No Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
hapticActuators
Chrome No Edge 15 — 79 Firefox Yes
Disabled
Yes
Disabled
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 Opera No Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
id
Chrome 35
35
不支持 21 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 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 Opera 22
22
不支持 15 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari No WebView Android No Chrome Android 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 32 Opera Android 22
22
不支持 14 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS No Samsung Internet Android 4.0
4.0
不支持 2.0 — 3.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
index
Chrome 35
35
不支持 21 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 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 Opera 22
22
不支持 15 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 32 Opera Android 22
22
不支持 14 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
不支持 2.0 — 3.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
mapping Chrome 35
35
不支持 21 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 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 Opera 22
22
不支持 15 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 32 Opera Android 22
22
不支持 14 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
不支持 2.0 — 3.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
pose
Chrome No Edge 15 — 79 Firefox Yes
Disabled
Yes
Disabled
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 Opera No Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
timestamp Chrome 35
35
不支持 21 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 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 Opera 22
22
不支持 15 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android 35
35
不支持 25 — 34
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 32 Opera Android 22
22
不支持 14 — 21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android 4.0
4.0
不支持 2.0 — 3.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit
vibrationActuator
非标
Chrome 68 Edge 79 Firefox No IE No Opera 55 Safari No WebView Android No Chrome Android 68 Firefox Android No Opera Android 48 Safari iOS No Samsung Internet Android 10.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。
要求使用供应商前缀或不同名称。
要求使用供应商前缀或不同名称。

另请参阅