Example: Connectivity
Plug and unplug your gamepad(s) and see the changes below:
Waiting...
In some browsers, you may need to press a button after plugging in the gamepad.
How it works
There are two event associated to gameControl that you can use:
connect: triggered when a gamepad is connected to the browser.disconnect: triggered when a gamepad is disconnected from the browser.
And an event handler can be easily associated to any of them using the
.on() method:
gameControl.on(EVENT_NAME, CALLBACK);
As an example, here is the code that manages the interaction above:
gameControl.on('connect', function() {
document.querySelector('#round-button').className = 'connected';
document.querySelector('#status').textContent = 'Device connected!';
});
gameControl.on('disconnect', function() {
document.querySelector('#round-button').className = 'disconnected';
document.querySelector('#status').textContent = 'Device disconnected!';
});