getGamepads method
Returns an array of gamepad objects that describe the state of each active gamepad device.
Syntax
var retval = navigator.getGamepads();Parameters
This method has no parameters.
Return value
Type: Array of gamepad
Each element in the result represents an active gamepad device.
Examples
This example shows one way to use the Gamepad API.
<!DOCTYPE html> <html> <head> <title>Gamepad API Sample</title> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <script> function gameLoop() { var gamepads = navigator.getGamepads(); for (var playerIndex = 0; playerIndex < gamepads.length; playerIndex++) { var gamepad = gamepads[playerIndex]; if (gamepad) { if (gamepad.buttons[6].pressed || gamepad.buttons[7].pressed) { // A trigger is pressed, fire weapon. fireWeapon(playerIndex); } } } window.requestAnimationFrame(gameLoop); } gameLoop(); </script> </head> <body> </body> </html>
See also
Show: