<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Yasine Bounaijat]]></title><description><![CDATA[Yasine Bounaijat]]></description><link>https://ybounaijat.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Dec 2026 00:16:42 GMT</lastBuildDate><atom:link href="https://ybounaijat.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Reading a game controller in the browser with the Gamepad API]]></title><description><![CDATA[I wanted a quick way to check if a controller's buttons and sticks work, without installing anything. Browsers can already read gamepads through the…]]></description><link>https://ybounaijat.hashnode.dev/reading-a-game-controller-in-the-browser-with-the-gamepad-api</link><guid isPermaLink="true">https://ybounaijat.hashnode.dev/reading-a-game-controller-in-the-browser-with-the-gamepad-api</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Tutorial]]></category><category><![CDATA[GameDev]]></category><dc:creator><![CDATA[Yasine Bounaijat]]></dc:creator><pubDate>Mon, 30 Nov 2026 10:12:57 GMT</pubDate><content:encoded><![CDATA[<p>I wanted a quick way to check if a controller's buttons and sticks work, without installing anything. Browsers can already read gamepads through the Gamepad API, so I built a small tester around it. Here is how the core of it works.</p>
<p>(Disclosure: I built ControllerTest, linked at the end.)</p>
<h2>1. Detect the controller</h2>
<p>Browsers hide gamepads until the user presses a button, so listen for the connect event:</p>
<pre><code class="language-js">window.addEventListener("gamepadconnected", (e) =&gt; {
  console.log("Connected:", e.gamepad.id);
});
window.addEventListener("gamepaddisconnected", (e) =&gt; {
  console.log("Disconnected:", e.gamepad.id);
});
</code></pre>
<p>If nothing fires, press a button on the pad. This is the most common "my controller is not detected" cause.</p>
<h2>2. Poll the state every frame</h2>
<p>The API has no input events for buttons or sticks. You read a snapshot each frame:</p>
<pre><code class="language-js">function loop() {
  const pads = navigator.getGamepads();
  const pad = pads[0];
  if (pad) {
    pad.buttons.forEach((b, i) =&gt; {
      if (b.pressed) console.log("Button", i, "value", b.value);
    });
    const [lx, ly, rx, ry] = pad.axes;
    console.log("Left stick", lx.toFixed(3), ly.toFixed(3));
  }
  requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
</code></pre>
<p>Call navigator.getGamepads() every frame. The objects can be stale copies in some browsers, so do not cache them.</p>
<h2>3. Understand the layout</h2>
<p>Browsers map most pads to the "standard" layout (check pad.mapping === "standard"). Button 0 is the bottom face button: A on Xbox, Cross on PlayStation. Axes 0 and 1 are the left stick, 2 and 3 the right stick, each from -1 to 1. Triggers show up as buttons with an analog value from 0 to 1.</p>
<h2>4. Spot problems</h2>
<p>Three simple checks catch most faults:</p>
<ul>
<li>A button that never lights up, flickers, or stays lit.</li>
<li>A stick that does not return to 0.000 at rest.</li>
<li>A trigger that never reaches 1.0 or does not return to 0.</li>
</ul>
<p>Small resting values on a stick are normal. Consistently larger ones can mean drift, but this is a rough check, not a hardware diagnosis.</p>
<h2>5. Privacy</h2>
<p>Everything runs client side. Nothing from the controller needs to be sent to a server.</p>
<h2>Try it</h2>
<p>I put this into a free tester that needs no sign-up: <a href="https://controllertest.com">https://controllertest.com</a></p>
<p>Feedback welcome in the comments.</p>
]]></content:encoded></item></channel></rss>