When a browser doesn't recognise a controller, the Gamepad API gives you its axes in whatever order the device reports them. Six numbers, no labels. Two of them are the right stick, two are the analog triggers, and which is which depends on the manufacturer.
Here is the standard order next to two raw ones, for axes 2 to 5:
| Pad | axes[2] | axes[3] | axes[4] | axes[5] |
|---|---|---|---|---|
| What the standard layout expects | right X | right Y | (none) | (none) |
| Sony, raw | right X | L2 | R2 | right Y |
| GameSir G7 Pro, Bluetooth, macOS | right X | RT | LT | right Y |
Assume the wrong one and you get the classic symptom: a right stick that sits jammed in one corner, because you are reading a trigger as a stick axis.

Same input on both sides: right stick circle, left trigger, right trigger. Left, the standard order is assumed and the stick is pinned. Right, the axes have been sorted.
The usual answer is a lookup table keyed by vendor and product ID. I use one for buttons. For axes I found I didn't need it, because the hardware gives the answer away for free.
The observation
Leave the controller alone on the desk and look at the numbers:
- A stick axis rests at 0. It's a spring-centred potentiometer or Hall sensor.
- A trigger axis rests at -1. It's the same -1..1 range, but its neutral position is one end of the travel.
So you don't need to know the model. You need to watch each axis for a moment when nobody is touching it.
The classifier
export class AxisRoles {
static readonly REST_MS = 300;
private role: ('stick' | 'trigger' | null)[] = [null, null, null, null];
private restAt: ('stick' | 'trigger' | null)[] = [null, null, null, null];
private since = [0, 0, 0, 0];
private locked: { rightStick: [number, number]; triggers?: [number, number] } | null = null;
resolve(axes: readonly number[], now = performance.now()) {
if (axes.length < 6) return { rightStick: [2, 3] };
if (this.locked) return this.locked;
for (let i = 0; i < 4; i++) {
if (this.role[i]) continue;
const v = Number(axes[i + 2]);
const at = v <= -0.9 ? 'trigger' : Math.abs(v) < 0.15 ? 'stick' : null;
if (at !== this.restAt[i]) {
this.restAt[i] = at;
this.since[i] = now;
} else if (at && now - this.since[i] >= AxisRoles.REST_MS) {
this.role[i] = at;
}
}
// ...
}
}
Each of the four unknown axes is watched separately. An axis gets a role when it has sat in one rest zone for 300 ms without leaving. Once it has a role it keeps it. When two sticks and two triggers have been found, the answer is locked and the function becomes a property read.
Four decisions in there are worth spelling out.
A dwell time, not a snapshot. Classifying on the first frame fails whenever the page loads while the player is mid-game: a stick pushed fully left reads -1 and looks exactly like a resting trigger. Holding a stick at a rail for 300 ms straight is possible, but an axis only needs to be caught resting once, and sticks pass through centre constantly. In practice the stick gets classified at centre long before it could be misread at the edge.
Zones, not exact values. -0.9 and 0.15 are generous on purpose. Worn sticks rest at 0.03 or 0.08. Some triggers idle at -0.98. A zone tolerant enough for tired hardware still leaves a wide dead band between the two, and an axis in that band is simply "not resting", so it gets no vote.
Per-axis state. Classifying all four in one go would mean waiting for a moment when the right stick and both triggers are released together. That moment may not come in a firefight. Independent timers mean each axis is settled the first time it personally rests.
Short reports are left alone. Fewer than six axes means there is no room for separate trigger axes, so the right stick is where the standard says it is.
The first 300 milliseconds
Before the roles are known, something still has to be drawn. Guessing is how you get the pinned stick. So the fallback is deliberately modest:
// Not known yet: show no trigger travel, and take the right stick from the
// two axes that are not sitting at a trigger's rest position.
const live = [2, 3, 4, 5].filter((n) => Number(axes[n]) > -0.9);
return { rightStick: live.length >= 2 ? [live[0], live[1]] : [2, 5] };
For a third of a second the triggers show nothing and the stick is read from whichever axes aren't parked at -1. On a resting pad that is already the right answer. The overlay never shows a stick slammed into a corner, which is the failure viewers notice.
What it can't tell you
The classifier knows there are two triggers. It doesn't know which is left.
The convention is that the lower-numbered axis is the left trigger, and Sony follows it. The GameSir in the table above doesn't: its left trigger is axis 4 and its right is axis 3. I only know because I pulled each trigger on the real pad and wrote down which number moved.
That one bit does come from a table. The SDL controller database lists lefttrigger:a4,righttrigger:a3 for that pad, and my build script reduces it to a single flag:
// 19th value: 1 when the left trigger sits on the higher-numbered axis of the two.
layout.push(lt >= 0 && rt >= 0 && lt > rt ? 1 : 0);
At run time the classifier finds the two trigger axes, and the flag says whether to swap them. Geometry from observation, handedness from data. For a pad in no table at all, the triggers may be mirrored until the user runs the calibrator, and I'd rather admit that than hide it.
There is a second honest limitation: this assumes sticks and triggers are the only things on axes 2 to 5. A flight stick with a throttle that rests wherever you left it will confuse it. I draw PS5, PS4 and Xbox pads, so I've decided that's out of scope; if you're writing a general-purpose tester you'll want an escape hatch.
Testing a time-based classifier
The class takes now as a parameter instead of reading the clock itself, which makes the tests boring in the good way:
const settled = () => {
const r = new AxisRoles();
r.resolve(REST, 0);
r.resolve(REST, 400);
return r;
};
Two calls 400 fake milliseconds apart and the roles are locked. No timers, no await, no flakes. The fixture REST is the actual resting report from the GameSir: [0, 0, 0, -1, -1, 0, 0, 0, 0, 3.286].
That last number, 3.286 on axis 9, is not a mistake. It's the D-pad, and it gets its own post next week.
The classifier runs in every controller overlay the site serves, so if you have an odd pad over Bluetooth, plugging it in is the quickest way to see whether your axes sort themselves out.
Top comments (0)