Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Gamepad API lets a browser game detect supported controllers and read their button and stick input. In JavaScript, listen for connection and disconnection events, then poll navigator.getGamepads() during the game loop for the latest state. It does not provide a game, graphics, controller pairing, or identical button layouts across devices.

How do I use a game controller in a browser game?

A browser game uses the Gamepad API to receive controller state; the game decides what that input does. The World Wide Web Consortium describes the interface as “a low-level interface that represents gamepad devices.” Its working draft focuses on gamepads and comparable controls, not richer motion, depth, video-analysis, or gesture-recognition systems. Read the W3C Gamepad Working Draft; it identifies itself as work in progress.

The usual implementation separates device lifecycle from live input: connection events tell your game that a controller is available, while a per-frame read supplies its current state. The event’s gamepad index identifies a slot in the array returned by navigator.getGamepads(). Read from that array in the update loop rather than assuming the object received when the event fired remains current. MDN’s guide describes this polling approach and connection behavior: Using the Gamepad API.

Detect a controller and read its current state

This minimal example tracks connected indices and checks the latest state on each animation frame. Replace the comments with your game’s input handling and rendering logic.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
LAPGEAR Home Office Pro Lap Desk - Black Carbon, Fits 15.6” Laptops
  • Spacious Design: Measuring 21.1" wide and 14.1" deep, our lap desk comfortably fits most laptops up to 15.6". Extra room for accessories ensures convenience.
  • Enhanced Functionality: Packed with handy features, including a 5x9" precision tracking mouse pad and a built-in phone slot for seamless work or video calls. Plus, enjoy ergonomic support with the integrated cushioned wrist rest.
  • Cool Comfort: Enjoy a stable surface with our lap desk's dual bolster cushion, designed for comfort and airflow, keeping your lap cool during extended use.
  • Durable Surface: Work with confidence on our lap desk's solid surface, featuring a sleek black carbon color, ensuring optimal air circulation to prevent your laptop from overheating.
  • On-the-Go Convenience: With an integrated handle and lightweight design (2.8 lbs), our lap desk is portable for travel or moving around the house, offering flexibility in any space.
const connected = new Set();

window.addEventListener("gamepadconnected", (event) => {
  connected.add(event.gamepad.index);
  console.log("Controller available", event.gamepad.index);
});

window.addEventListener("gamepaddisconnected", (event) => {
  connected.delete(event.gamepad.index);
  console.log("Controller disconnected", event.gamepad.index);
});

function update() {
  for (const index of connected) {
    const gamepad = navigator.getGamepads()[index];
    if (!gamepad || !gamepad.connected) continue;

    // Read current values here, not just when the connection event fires.
    const buttons = gamepad.buttons;
    const axes = gamepad.axes;
    // Map buttons and axes to game actions, then update the game.
  }

  requestAnimationFrame(update);
}

requestAnimationFrame(update);

In production, run the loop when the game needs updates and pause or adjust it when it does not. Handle an empty or unavailable slot safely: a controller can disconnect between frames, and the array can contain empty entries.

Read buttons and axes

The W3C draft describes buttons as values in the range 0 to 1 and axes as values from -1 to 1. A button object exposes state such as its value and whether it is pressed; axes are numeric values. These are raw inputs, not game actions. Your game must decide, for example, which button means “jump” and how much stick movement counts as movement.

Rank #2
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Check gamepad.mapping to learn whether the browser applied a known layout. A recognized mapping can make standard controls easier to handle, but it is not a promise that every controller has the same ordering. For unmapped devices, provide a configurable or device-specific mapping as appropriate. Small unintended stick values may also need a dead zone; threshold selection is a game-design choice, not behavior the API guarantees.

Why is my controller not showing up in the browser?

Being physically connected and being exposed to a particular page are different states. A newly connected controller can trigger gamepadconnected for the focused page. If the controller was already connected when the page loaded, MDN says the event is delivered after the user presses a button or moves an axis. In Firefox, a page sees a gamepad only after the user interacts with it while the page is visible; MDN describes this as a privacy measure intended to reduce fingerprinting. MDN’s usage guide covers these behaviors.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Yilador Webcam Cover 3 Pack, 0.03 inch Ultra Thin Laptop Camera Cover Slide
  • Note: Not suitable for MacBooks released after 2023 or devices with a protruding front camera; Not applicable to full-screen or notch-style tempered glass screen protectors; Do not use on the rear camera of the phone.
  • 💻 Why Do You Need a Webcam Cover Slide? — Safeguard your privacy by covering your webcam with our reliable webcam cover when not in use. Don't let anyone secretly watch you. Stay protected!
  • ✅ Thin & Stylish — Enhance your laptop's functionality and aesthetics with our 0.027" ultra-thin webcam covers. Seamlessly close your laptop while adding a touch of sophistication.
  • ✅ Fits Most Devices — Compatible with laptops, phones, tablets, desktops! Keep your privacy intact on Ap/ple, Mac/Book, iPh/one, iP/ad, H/P, L/novo, De/ll, Ac/er, As/us, Sa/msung devices.
  • ✅ 365 Days Protection — Our upgraded 3.0 adhesive ensures a strong hold that won't damage your equipment. Experience reliable, long-term privacy protection day in and day out.

Give players a visible prompt such as “Press any controller button to connect,” and keep keyboard or touch controls available so the prompt is usable. If the controller is not detected, check these conditions:

  • Page focus and visibility: Bring the game tab to the foreground and press a controller button or move a stick.
  • Event registration: Install connection and disconnection listeners early, and log events while diagnosing the page.
  • Current state: Call navigator.getGamepads() in the update loop and check the relevant index; do not rely only on an earlier event object.
  • Embedded game policy: If the game is inside an iframe, inspect the site’s Permissions-Policy response header and the iframe’s allow attribute. A policy blocking gamepad can make getGamepads() throw a SecurityError and prevent connection events. MDN lists the directive’s default allowlist as *, but a site can restrict it; MDN also labels this directive experimental. MDN: Permissions-Policy: gamepad directive.
  • Device and platform: Try the browser, operating system, and connection method the game supports. Controller capabilities and browser exposure can differ across combinations.

Does the Gamepad API work on all browsers?

MDN marks the core Gamepad API as widely available across browsers since March 2017. That is an availability milestone, not a usage or adoption statistic, and it does not mean every controller or feature works in every browser-and-platform pairing. The API’s events, device properties, and optional capabilities can have different support. See MDN’s Gamepad API reference and its Gamepad interface reference.

Rank #4
AboveTEK Portable Laptop Lap Desk w/Retractable Left/Right Mouse Pad Tray, Non-Slip Heat Shield Tablet Notebook Computer Stand Table w/Sturdy Stable Work Surface for Bed Sofa Couch or Travel
  • Anti-Slip Surface - Transform your laptop into a mobile workstation with the AboveTEK portable laptop lap desk. The anti-slip surface provides a strong grip for laptops up to 15.6 inches(Diagonal), while the double rubber strip on the bottom ensures a stable display or typing experience on your lap, couch, or bed.
  • Retractable Mouse Pad - Retractable laptop mouse pad extends on both directions for the left/right handed with elevation along the edges for stopping mouse from falling off. The size of laptop tray is 14" X 9.7" and the size of mouse pad is 7.4" X 6.1".
  • Effective Heat Shield - The effective heat shield made of sturdy and thick material protects your laptop from overheating. Prioritizes your comfort and safety, an ideal lap pad or board for working anywhere.
  • EASY to Carry and Store - With an ergonomic and simplistic design, the lap desk is portable to store in a backpack. Only 15" in size, 2.2 lb of weight and with slim 0.6 inch thickness, it is ready to be easily carried around.
  • Widely Applicable - The smooth platform accommodates laptops and tablets up to 15.6 inches(Diagonal), making it a versatile accessory and one of the best gifts for mom, dad, students and professionals. Perfect for use as a laptop bed tray or tablet holder anywhere at home, library, or park.

Test the combinations that matter to your audience: browser, operating system, controller, and connection type. In particular, do not make haptic feedback essential to play. A controller may support vibration in hardware while the browser or platform combination does not expose it through the API.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which input approach should a browser game use?

Approach What it does Trade-off
Connection events Tracks when a controller becomes available or disconnects. Useful for lifecycle changes, but does not replace reading current input.
Per-frame polling Reads fresh button and axis state through navigator.getGamepads(). Fits a game update loop; pause or adjust polling when the game is not active.
Known mapping Uses a browser-reported standard layout when one is available. Reduces layout-specific work, but does not guarantee identical behavior for every device.
Custom mapping Lets a game account for layouts not covered by a known mapping. Requires the game to decide how device-specific controls become actions.
Keyboard or touch fallback Lets players use the game without a controller. Requires a second input path, but avoids making controller detection a barrier to starting.
Optional haptics Adds feedback where supported. Availability varies by hardware, browser, and platform; core gameplay should work without it.
Top-level page Runs the game outside an embedded frame. Avoids iframe-specific policy configuration.
Embedded iframe Runs the game within another page. Permissions Policy can restrict access, so the host page may need to allow the feature.

What the Gamepad API does not provide

The API exposes input information; it does not pair a controller, install drivers, remap controls at the operating-system level, or supply a game engine. Those responsibilities belong to the browser, operating system, game, or separate software as applicable. Build the game’s core controls around buttons and axes, make optional features genuinely optional, and offer another way to play when a controller is absent or unavailable.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Sale
LAPGEAR Home Office Lap Desk – Pink, Fits 15.6” Laptops
  • Spacious Design: Measuring 21.1" wide and 12" deep, our lap desk comfortably fits most laptops up to 15.6". Extra room for accessories ensures convenience.
  • Enhanced Functionality: Packed with handy features, including a 5x9" precision tracking mouse pad and a built-in phone slot for seamless work or video calls. Plus, enjoy laptop support with the integrated device ledge.
  • Cool Comfort: Enjoy a stable surface with our lap desk's dual bolster cushion, designed for comfort and airflow, keeping your lap cool during extended use.
  • Durable Surface: Work with confidence on our lap desk's solid surface, featuring a blush pink color, ensuring optimal air circulation to prevent your laptop from overheating.
  • On-the-Go Convenience: With an integrated handle and lightweight design (2.14 lbs), our lap desk is portable for travel or moving around the house, offering flexibility in any space.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.