GamepadButton 接口定义了在一个手柄或其他控制器的唯一的一个按键,允许访问不同控制器设备可用类型的按钮的当前状态。

GamepadButton 对象是由 Gamepad 接口的 buttons 属性返回的可查询任意值的数组返回的。

注:上述情况是在 Firefox Gecko 28 及以上的;Chrome 和较早版本的 Firefox 访问此属性时仍然会返回一个双精浮点值的数组。

属性

GamepadButton.value 只读
一个用来表示按钮当前状态的双精浮点值,比如说许多现代控制器都有的扳机键。其值被规范至范围 0.0 —1.0 之间,其中 0.0 表示按钮没有被按下,而 1.0 表示按钮被完全按下 (按到底)。
GamepadButton.pressed 只读
一个指示当前按钮是被按下 (true) 还是没有被按下 (false) 的布尔值。

示例

接下来的代码来自于我 (文作者) 的 Gamepad API 按钮示例 (你可以在线查看示例,并在 Github 上查找源代码。) 注意代码分支——在 Chrome 中Navigator.getGamepads 需要一个 webkit 前缀并且按钮值被存储为一个双精浮点值的数组,然而在 Firefox 中 Navigator.getGamepads 不需要前缀,且按钮值被存储为 GamepadButton 对象数组;其中有我们需要的  GamepadButton.value 或 GamepadButton.pressed 属性,这取决于他们是什么类型的按钮。在这个简单的示例中我只是允许了它们。

function gameLoop() {
  if(navigator.webkitGetGamepads) {
    var gp = navigator.webkitGetGamepads()[0];

    if(gp.buttons[0] == 1) {
      b--;
    } else if(gp.buttons[1] == 1) {
      a++;
    } else if(gp.buttons[2] == 1) {
      b++;
    } else if(gp.buttons[3] == 1) {
      a--;
    }
  } else {
    var gp = navigator.getGamepads()[0];

    if(gp.buttons[0].value > 0 || gp.buttons[0].pressed == true) {
      b--;
    } else if(gp.buttons[1].value > 0 || gp.buttons[1].pressed == true) {
      a++;
    } else if(gp.buttons[2].value > 0 || gp.buttons[2].pressed == true) {
      b++;
    } else if(gp.buttons[3].value > 0 || gp.buttons[3].pressed == true) {
      a--;
    }
  }

  ball.style.left = a*2 + "px";
  ball.style.top = b*2 + "px";

  var start = rAF(gameLoop);
};

规范

规范 状态 备注
Gamepad
GamepadButton
Working Draft Initial definition

浏览器兼容性

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 YesEdge Mobile Full support YesFirefox Android Full support 32Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
pressedChrome 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 YesEdge Mobile Full support YesFirefox Android Full support 32Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
valueChrome 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 YesEdge Mobile Full support YesFirefox Android Full support 32Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
touchedChrome ? Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
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.

另请参阅

使用 Gamepad API

文档标签和贡献者

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