Gamepad / joystick responses in OSWeb with the browser Gamepad API: anyone using this?
Hi all,
I saw the older thread about using a joystick in an online (OSWeb/JATOS) experiment, where the built-in gamepad item isn't supported. I'm looking at the same thing for a simple go/no-go task and wanted to share what I've found so far and ask if anyone has a cleaner setup.
The browser exposes controllers through navigator.getGamepads(). A few things that tripped me up:
- The browser only "sees" the controller after the participant presses a button once on the page, so the instructions screen needs a line like "press any button on your controller to continue".
- There is no event per button press, only a snapshot of the current state, so you have to poll it (e.g. with requestAnimationFrame) and compare it with the previous frame.
- Sticks are never exactly 0 at rest. Worn controllers can sit at 0.05 to 0.15, so I'd use a deadzone of around 0.2 before counting a stick movement as a response.
- Button order differs between controllers and browsers. Checking gamepad.mapping === "standard" helps, but it's worth testing the participant's controller first.
Rough polling idea for an inline_javascript item:
let prev = []
function poll() {
const gp = navigator.getGamepads()[0]
if (gp) {
gp.buttons.forEach((b, i) => {
if (b.pressed && !prev[i]) console.log('button', i, performance.now())
prev[i] = b.pressed
})
}
requestAnimationFrame(poll)
}
requestAnimationFrame(poll)
Timing is limited by the browser's frame rate (about 16 ms at 60 Hz) plus the controller's own polling, so it's fine for go/no-go but probably not for precise RT work.
Has anyone got this working inside OSWeb's run phase, so the response ends the trial and gets logged as a normal response variable? And does anyone know if Bluetooth controllers add noticeable latency compared to USB in practice?
Thanks!
Free online controller tester: https://gamepadtesters.net/
