Skip to content

Instantly share code, notes, and snippets.

@jpenney1
Created May 9, 2018 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpenney1/b3bd9c3a3866e998d4dda3610604297b to your computer and use it in GitHub Desktop.
Save jpenney1/b3bd9c3a3866e998d4dda3610604297b to your computer and use it in GitHub Desktop.
with this snippet, you can add an event listener to any webpage which will log the button pressed to the console, along with its entire availbale buttons array and index at which it was found. Anyone know how if there's a naive way to determine which button in the array is which?
var gameLoop = function(res) {
var gamepad = navigator.getGamepads();
navigator.getGamepads()[0].buttons.forEach(function(button, i, buttons) {
if(button.pressed) console.log('pressed: ', buttons.slice(),' i: ', i, buttons[i])
});
requestAnimationFrame(gameLoop)
}
window.addEventListener('gamepadconnected', gameLoop)
// src: https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment