Navigator.getGamepads()
Experimental
这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
调用 Navigator.getGamepads()
方法会返回一个数组:第一个值为 null
,其他的值均为 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 |
---|---|---|
Gamepad The Gamepad API specification |
Working Draft | 初始定义 |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
General support |
21.0 webkit |
(Yes) | 29.0 (29.0) [1] | 未实现 |
15.0 webkit |
未实现 |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
General support | 未实现 | (Yes) | 未实现 | 未实现 | 未实现 | 未实现 |
[1] 自 Firefox 24 起可以通过偏好设置启用。