GamepadButton
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The 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
property of the Gamepad
interface.
Properties
-
GamepadButton.value
Read only -
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.touched
Read only -
A boolean value indicating whether the button is currently touched (
true
) or not touched (false
). -
GamepadButton.pressed
Read only -
A boolean value indicating whether the button is currently pressed (
true
) or unpressed (false
).
Example
The button values in the following example are stored as an array of GamepadButton
objects. This simple example checks to see if the GamepadButton.value
of a button is greater than 0
, or if the GamepadButton.pressed
property indicates the button has been pressed.
function gameLoop() { let 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"; let start = rAF(gameLoop); };
Specifications
Specification |
---|
Gamepad # gamepadbutton-interface |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
GamepadButton |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
Yes |
32 |
22
14-21
|
10.3 |
Yes |
pressed |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
Yes |
32 |
22
14-21
|
10.3 |
Yes |
secure_context_required |
86 |
86 |
91 |
No |
72 |
No |
No |
86 |
91 |
61 |
No |
No |
touched |
73 |
15 |
55 |
No |
60 |
10.1 |
No |
73 |
55 |
52 |
10.3 |
11.0 |
value |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
Yes |
32 |
22
14-21
|
10.3 |
Yes |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/GamepadButton