GamepadButton interface defines an individual button of a gamepad or other controller, allowing access to the current state of different types of buttons available on the control device.

A GamepadButton object is returned by querying any value of the array returned by the buttons 特性为 Gamepad 接口。

注意 : This is the case in Firefox Gecko 28 and later; Chrome and earlier Firefox versions still return an array of double values when this property is accessed.

特性

GamepadButton.value 只读

A double value used to represent the current state of analog buttons, such as the triggers on many modern gamepads. The values are normalized to the range 0.0 —1.0, with 0.0 representing a button that is not pressed, and 1.0 representing a button that is fully pressed.

GamepadButton.pressed 只读
布尔 value indicating whether the button is currently pressed ( true ) or unpressed ( false ).

范例

The following code is taken from my Gamepad API button demo (you can view the demo live ,和 find the source code on Github.) Note the code fork — in Chrome Navigator.getGamepads needs a webkit prefix and the button values are stored as an array of double values, whereas in Firefox Navigator.getGamepads doesn't need a prefix, and the button values are stored as an array of GamepadButton objects; it is the GamepadButton.value or GamepadButton.pressed properties of these we need to access, depending on what type of buttons they are. In this simple example I've just allowed either.

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
The definition of 'GamepadButton' in that specification.
工作草案 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
GamepadButton Chrome 35
35
不支持 21 — 34 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge ≤18 Firefox 29
29
不支持 24 — 28 Disabled
Disabled ). To change preferences in Firefox, visit
IE No Opera 22
22
不支持 15 — 21 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android Yes Firefox Android 32 Opera Android 22
22
不支持 14 — 21 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android Yes
pressed Chrome 35
35
不支持 21 — 34 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 24 — 28 Disabled
Disabled ). To change preferences in Firefox, visit
IE No Opera 22
22
不支持 15 — 21 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android Yes Firefox Android 32 Opera Android 22
22
不支持 14 — 21 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android Yes
touched Chrome Yes Edge 15 Firefox Yes IE No Opera ? Safari 10.1 WebView Android No Chrome Android Yes Firefox Android Yes Opera Android No Safari iOS 10.3 Samsung Internet Android Yes
value Chrome 35
35
不支持 21 — 34 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 29
29
不支持 24 — 28 Disabled
Disabled ). To change preferences in Firefox, visit
IE No Opera 22
22
不支持 15 — 21 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 10.1 WebView Android No Chrome Android Yes Firefox Android 32 Opera Android 22
22
不支持 14 — 21 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 10.3 Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

Using the Gamepad API

元数据

  • 最后修改:
  1. 手柄 API
  2. GamepadButton
  3. 特性
    1. pressed
    2. value
  4. Related pages for Gamepad API
    1. Gamepad
    2. GamepadEvent
    3. Navigator.getGamepads()
    4. Window.ongamepadconnected
    5. Window.ongamepaddisconnected

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1