Lord Goderick Lord Goderick - 3 years ago 122
Javascript Question

Gamepad API "gamepadconnected" not working

I am using the Gamepad API "gamepadconnected" function which is not working properly. However, the "gamepaddisconnected" function works for some reason. Upon connecting my bluetooth controller, nothing shows in the console, however, it does show the information displayed when disconnected.

What I have here is very basic and standard:

window.addEventListener("gamepadconnected", function(e) {
console.log("Controller connected");
});

window.addEventListener("gamepaddisconnected", function(e) {
console.log("Controller disconnected");
});


What am I missing here? Where did go wrong? I made sure the first focused page is the one with the scripts above. Thanks for your help.

Answer Source

MDN states that "If a gamepad is already connected when the page loaded, the gamepadconnected event is dispatched to the focused page when the user presses a button or moves an axis.". So your page will not always receive the gamepadconnected event when you connect the pad. https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API

So it's best to e.g. let the user press a button to get your application started. This is also useful when there are multiple gamepads connected to the computer and your application has to know which one to use.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download