Chrome has no gamepadconnected event

I've been using html5gamepad.com just off-and-on through development of EELemental. And reviewing tutorials for gamepad implementation.

All websites trying to provide guidance to implementing gamepads in HTML5 games, have code that works with Window.gamepadconnected even. Unfortunately in the latest version Chrome (Version 58.0.3029.110), this event doesn't fire.

Correction: gamepadconnected does not fire when connecting a gamepad. It *does* fire when DISconnecting a gamepad. Hey! All the tutorial web sites that want you following their guidance say there's also Window.gamepaddisconnected (https://developer.mozilla.org/en-US/docs/Web/Event...) but... yeah it doesn't ever fire.

So the gamepadconnected event fires when disconnecting a gamepad, and the gamepaddisconnected event never fires.

This is taking longer than I have patience for tonight.

Comments

Log in with your itch.io account to leave a comment.

(Edited 1 time)

It's true. I have same problem.

https://developer.mozilla.org/en-US/docs/Web/API/G...

Note: These events are not fired in Chrome, only Firefox. In Chrome you have to use Navigator.getGamepads() to access Gamepad objects.

ps

You need use navigator.getGamepads() in your update loop function

My example for browser console:

//gamepads
let pad0 = null;
let pad1 = null;
let pad2 = null;
let pad3 = null;

// gamepads connected status
let gamepadConnected = {
0: false, // false == pad0 disconnected, true == pad0 connected
1: false, // false == pad1 disconnected, true == pad1 connected
2: false,
3: false,
};

//connect callback
function padConnect(pad) {
console.log('gamepad connected, index: ' + (pad.index));
}

//disconnect callback
function padDisconnect(padIndex) {
console.log('gamepad disconnected, index: ' + (padIndex));
}

//'gamepadconnected' and 'gamepaddisconnected' events
function padConnectionCheck() {
//pad0
if (pad0 !== null && !gamepadConnected[0]) {
gamepadConnected[0] = true; // set 'connected' status
padConnect(pad0); //connected callback
} else if (pad0 === null && gamepadConnected[0]) {
gamepadConnected[0] = false; // set 'disconnected' status
this.padDisconnect(0); //'disconnected' callback you need to use index of gamepad, because gamepad == null
}
//pad1
if (pad1 !== null && !gamepadConnected[1]) {
gamepadConnected[1] = true;
padConnect(pad1);
} else if (pad1 === null && gamepadConnected[1]) {
gamepadConnected[1] = false;
this.padDisconnect(1);
}
//pad2
if (pad2 !== null && !gamepadConnected[2]) {
gamepadConnected[2] = true;
padConnect(pad2);
} else if (pad2 === null && gamepadConnected[2]) {
gamepadConnected[2] = false;
this.padDisconnect(2);
}
//pad3
if (pad3 !== null && !gamepadConnected[3]) {
gamepadConnected[3] = true;
padConnect(pad3);
} else if (pad0 === null && gamepadConnected[3]) {
gamepadConnected[3] = false;
this.padDisconnect(3);
}
}

//gamepad update loop

function gamepadUpdate() {
pad0 = navigator.getGamepads()[0];
pad1 = navigator.getGamepads()[1];
pad2 = navigator.getGamepads()[2];
pad3 = navigator.getGamepads()[3];

padConnectionCheck();

}
setInterval(gamepadUpdate, 50);

//game update loop
function gameUpdate() {
if (pad0 !== null && pad0.buttons[/*button index 0-14*/ 14].pressed) {
//do something
console.log('Dpad Left Pressed');
}
}
setInterval(gameUpdate, 50);