Gamepad是Gamepad API的接口,定义为一个独立的游戏手柄或其他控制器,允许访问如按钮按下,方位和id等信息。
一个Gamepad对象能以两种方式返回:通过gamepad的gamepadconnected和gamepaddisconnected事件属性,或者获取Navigator.getGamepads()方法中返回的数组中的任意值。
属性
Gamepad.displayId只读- 返回
VRDisplay.displayIdof the associatedVRDisplay— theVRDisplaythat the gamepad is controlling the displayed scene of. Gamepad.id只读- A
DOMStringcontaining 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.connected只读- A boolean indicating whether the gamepad is still connected to the system.
Gamepad.buttons只读- An array of
gamepadButtonobjects representing the buttons present on the device. Gamepad.axes只读- An array representing the controls with axes present on the device (e.g. analog thumb sticks).
Gamepad.timestamp只读- A
DOMHighResTimeStamprepresenting the last time the data for this gamepad was updated.
例子
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);
});
规格
| Specification | Status | Comment |
|---|---|---|
| Gamepad Gamepad |
Working Draft | Initial defintion |
| Unknown displayId |
Unknown | Defines the Gamepad.displayId property. |
浏览器兼容性
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| General support | 21.0 webkit 35.0 |
(Yes) | 29.0 (29.0) | 未实现 | 15.0 webkit 22.0 |
未实现 |
| Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| General support | 未实现 | ? | 32.0 (32.0) | 未实现 | 未实现 | 未实现 |