User Agents

Web browsers and everyday web tech, explained

Chrome Gamepad API: How to Use It

· 6 min read · Browsers

In this article
  1. What the Gamepad API Actually Provides
  2. How to Detect and Read a Gamepad in Chrome
  3. Understanding Button and Axis Indexes
  4. How to Choose the Right Polling Loop
  5. Common Problems and How to Avoid Them
  6. Comparisons: Chrome vs. Other Browsers and Native
  7. Alternatives to the Gamepad API
  8. Frequently Asked Questions

To use the Gamepad API in Chrome, you call navigator.getGamepads() to read the current state of connected controllers, and listen for the gamepadconnected and gamepaddisconnected events to detect when a controller is plugged in or unplugged. The API is read-only and event-driven: you poll the gamepad objects inside a requestAnimationFrame loop to get button presses and stick movements, then map those values to actions in your web page or game.

What the Gamepad API Actually Provides

The Gamepad API is a browser standard that exposes physical game controllers (like Xbox, PlayStation, or generic USB gamepads) to JavaScript. It does not handle input interpretation, vibration (except for the vibrationActuator on some gamepads), or device pairing—those are left to the browser and OS. In Chrome, the API is available on secure contexts (HTTPS) and works on desktop and Android. It gives you three main things:

  • Gamepad objects – each has an id, index, connected, mapping (standard or empty), and arrays for buttons and axes.
  • Button states – each button has pressed, touched, and value (for analog triggers).
  • Axis states – each axis is a float from -1.0 to 1.0 (e.g., left stick x/y).

How to Detect and Read a Gamepad in Chrome

The core usage is straightforward. First, listen for connection events:

window.addEventListener('gamepadconnected', (e) => {
  console.log('Connected:', e.gamepad.id);
});
window.addEventListener('gamepaddisconnected', (e) => {
  console.log('Disconnected:', e.gamepad.id);
});

Then, in your main loop (usually requestAnimationFrame), get the current state and read inputs:

function loop() {
  const gamepads = navigator.getGamepads();
  const pad = gamepads[0]; // first connected gamepad
  if (pad) {
    // Buttons: index 0 is usually A/B bottom button
    if (pad.buttons[0].pressed) {
      // do something
    }
    // Axes: left stick horizontal is axes[0]
    const x = pad.axes[0];
  }
  requestAnimationFrame(loop);
}
requestAnimationFrame(loop);

Important: navigator.getGamepads() returns an array that may contain null slots for disconnected gamepads. Always check for null before accessing properties. The gamepadconnected event fires once per connection, but you still need to poll inside the loop to get live input—the API does not push button events.

Understanding Button and Axis Indexes

The mapping property tells you if the gamepad follows the standard layout. If mapping === 'standard', the button and axis indices are consistent across most modern controllers:

  • Buttons: 0 = bottom face (A on Xbox, X on PS), 1 = right face (B, Circle), 2 = left face (X, Square), 3 = top face (Y, Triangle), 4 = left bumper, 5 = right bumper, 6 = left trigger, 7 = right trigger, 8 = back, 9 = start, 10 = left stick press, 11 = right stick press, 12 = D-pad up, 13 = D-pad down, 14 = D-pad left, 15 = D-pad right.
  • Axes: 0 = left stick X, 1 = left stick Y, 2 = right stick X, 3 = right stick Y. On many gamepads, axes 4 and 5 are the analog triggers if they are exposed as axes rather than buttons.

If mapping is empty, the layout is vendor-specific—you may need to probe values to find which index corresponds to which physical control. For testing, you can log pad.id and the button/axis arrays to see what your controller reports.

How to Choose the Right Polling Loop

Because the Gamepad API is pull-based, you must decide how often to read it. The common pattern is to use requestAnimationFrame, which runs at the display refresh rate (typically 60Hz). This is ideal for games because input updates are synchronized with rendering. However, if you only need to detect button presses for a UI, you could poll less frequently (e.g., every 100ms) to save CPU. There is no built-in event for individual button presses, so you must track previous state to detect edges (press vs. release):

let prevButtons = [];
function loop() {
  const pad = navigator.getGamepads()[0];
  if (pad) {
    for (let i = 0; i < pad.buttons.length; i++) {
      const pressed = pad.buttons[i].pressed;
      if (pressed && !prevButtons[i]) {
        console.log('Button ' + i + ' pressed');
      }
      prevButtons[i] = pressed;
    }
  }
  requestAnimationFrame(loop);
}

Common Problems and How to Avoid Them

  • Gamepad not detected – Make sure the page is loaded over HTTPS (or localhost). Chrome requires a secure context for the Gamepad API. Also, some browsers only fire gamepadconnected after the user presses a button on the gamepad (this is a security measure to prevent fingerprinting). If you don't see the event, ask the user to press any button.
  • Stale data – navigator.getGamepads() returns the same array reference each time, but the gamepad objects are updated only when you call it. Always call it fresh inside your loop; do not cache the gamepad object across frames.
  • Axes values not centered – Some controllers have slight drift. You can apply a dead zone: if the absolute value of an axis is less than 0.1, treat it as 0.
  • Multiple gamepads – Use the index property to distinguish them. The array may have gaps; iterate over all entries and filter out nulls.

Comparisons: Chrome vs. Other Browsers and Native

Chrome’s implementation is close to the W3C specification, but there are minor differences. Firefox also supports the Gamepad API, but historically its support for the vibrationActuator was missing. Safari added Gamepad support in version 16.4, but its implementation may have quirks with certain controllers. The API is not available in Internet Explorer. Compared to native game development (e.g., using C++ with SDL), the Gamepad API is simpler but higher-level: you get a unified interface without dealing with OS-specific drivers. However, it lacks advanced features like force feedback (except for simple vibration on some gamepads) and low-latency raw input. For most web games, the API is sufficient.

Alternatives to the Gamepad API

  • Keyboard and mouse – The simplest alternative for desktop web games. Use keydown/keyup events.
  • Touch events – For mobile, you can use touch or pointer events to create virtual joysticks.
  • WebXR Gamepad API – If you are building VR/AR experiences, the WebXR API provides its own gamepad access for controllers like Oculus Touch.
  • Third-party libraries – Libraries like html5-gamepad or gamepad.js can normalize cross-browser differences, but they still rely on the underlying Gamepad API.

Frequently Asked Questions

Why doesn't my gamepad fire the 'gamepadconnected' event?

Chrome may delay firing the event until the user presses a button on the gamepad. This is a privacy measure to prevent websites from detecting gamepads without user interaction. Press any button on the controller, and the event should fire. Also ensure the page is served over HTTPS (or localhost) and that the gamepad is properly connected to your OS.

Can I use the Gamepad API on Chrome for Android?

Yes, Chrome on Android supports the Gamepad API, but you need to connect a physical controller via Bluetooth or USB. The experience is similar to desktop, but note that some Android devices may not support all buttons or axes. Also, the secure context requirement still applies.

How do I detect a button press only once, not repeatedly?

You need to track the previous state of each button. In your polling loop, compare the current pressed value to the previous frame’s value. If it changed from false to true, that’s a new press. This is a common pattern in game development called “edge detection.”

What does the 'mapping' property mean?

If mapping is 'standard', the button and axis indices follow the W3C standard layout, which matches Xbox and PlayStation controllers. If it’s an empty string, the controller uses a non-standard layout, and you must probe the inputs to figure out which index corresponds to which physical control. Most modern controllers report 'standard'.

Can I use the Gamepad API to read the PlayStation controller's touchpad?

No, the standard Gamepad API does not expose touchpad data. It only exposes buttons and axes. The touchpad on a DualShock 4 or DualSense is not part of the standard gamepad mapping. You would need a separate API or a browser extension to access that data.