Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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

navigator

 

 

Show:
© 2017 Microsoft