Gamepad
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The Gamepad
interface of the Gamepad API defines an individual gamepad or other controller, allowing access to information such as button presses, axis positions, and id.
A Gamepad object can be returned in one of two ways: via the gamepad
property of the gamepadconnected
and gamepaddisconnected
events, or by grabbing any position in the array returned by the Navigator.getGamepads()
method.
Properties
-
Gamepad.axes
Read only -
An array representing the controls with axes present on the device (e.g. analog thumb sticks).
-
Gamepad.buttons
Read only -
An array of
gamepadButton
objects representing the buttons present on the device. -
Gamepad.connected
Read only -
A boolean indicating whether the gamepad is still connected to the system.
-
Gamepad.displayId
Read only -
Returns the
VRDisplay.displayId
of an associatedVRDisplay
(if relevant) — theVRDisplay
that the gamepad is controlling the displayed scene of. -
Gamepad.hand
Read only -
An enum defining what hand the controller is being held in, or is most likely to be held in.
-
Gamepad.hapticActuators
Read only -
An array containing
GamepadHapticActuator
objects, each of which represents haptic feedback hardware available on the controller. -
Gamepad.id
Read only -
A
DOMString
containing identifying information about the controller. -
Gamepad.index
Read only -
An integer that is auto-incremented to be unique for each device currently connected to the system.
-
Gamepad.mapping
Read only -
A string indicating whether the browser has remapped the controls on the device to a known layout.
-
Gamepad.pose
Read only -
A
GamepadPose
object representing the pose information associated with a WebVR controller (e.g. its position and orientation in 3D space). -
Gamepad.timestamp
Read only -
A
DOMHighResTimeStamp
representing the last time the data for this gamepad was updated.
Example
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); });
Specifications
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 | |
Gamepad |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
35
25-34
|
32 |
22
14-21
|
10.3 |
4.0
2.0-3.0
|
axes |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
35
25-34
|
32 |
22
14-21
|
10.3 |
4.0
2.0-3.0
|
buttons |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
35
25-34
|
32 |
22
14-21
|
10.3 |
4.0
2.0-3.0
|
connected |
35
25-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
35
25-34
|
32 |
22
14-21
|
10.3 |
4.0
2.0-3.0
|
displayId |
55-80
Only works on desktop in an experimental version of Chrome (other builds won't return any devices when
Navigator.getVRDisplays() is invoked). |
79-80
15-79
|
55
Windows support was enabled in Firefox 55.
64
macOS support was enabled in Firefox 64.
|
No |
No |
No |
No |
55-80
Currently supported only by Google Daydream.
|
55 |
? |
No |
6.0-13.0
Currently supported only by Google Daydream.
|
hand |
No |
15-79 |
55 |
No |
No |
No |
No |
No |
55 |
No |
No |
No |
hapticActuators |
No |
15-79 |
55 |
No |
No |
No |
No |
No |
55 |
No |
No |
No |
id |
35
21-34
|
12 |
29 |
No |
22
15-21
|
No |
No |
35
25-34
|
32 |
22
14-21
|
No |
4.0
2.0-3.0
|
index |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
35
25-34
|
32 |
22
14-21
|
10.3 |
4.0
2.0-3.0
|
mapping |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
35
25-34
|
32 |
22
14-21
|
10.3 |
4.0
2.0-3.0
|
pose |
No |
15-79 |
55 |
No |
No |
No |
No |
No |
55 |
No |
No |
No |
secure_context_required |
86 |
86 |
91 |
No |
72 |
No |
No |
86 |
91 |
61 |
No |
No |
timestamp |
35
21-34
|
12 |
29 |
No |
22
15-21
|
10.1 |
No |
35
25-34
|
32 |
22
14-21
|
10.3 |
4.0
2.0-3.0
|
vibrationActuator |
68 |
79 |
No |
No |
55 |
No |
No |
68 |
No |
48 |
No |
10.0 |
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/Gamepad