'Using gamepad-api via events, rather than polling

I am building a chrome extension that would enable the use of my 3d-mouse (made by 3d connexion) to scroll web pages. The built-in 3d connexion drivers are worthless, and the 3d mouse presents itself as a 2 button, 6 axis gamepad to the OS.

The implementation is currently working using the below method:

function get3DMouseData() {
    if (document.hasFocus()) {
        var gp = navigator.getGamepads()[0]
        // axis 5 controls scroll speed
        if (gp.axes[5]) window.scrollBy(0,Math.round(gp.axes[5]*10)*10)
    }
}

interval = this.setInterval(get3DMouseData, 50)

It doesn't seem to be the smartest strategy to run get3DMouseData 20 times por second, and an event-based implementation would probably be more efficient.

  1. Is there an event-based methods for the gamepad-api that I can use in the case above?
  2. This implementation does not work properly in certain sites (like gmail). What I am doing wrong?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source