Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EncodeTheCode/a9915d835dc202af1becde4e35d13aff to your computer and use it in GitHub Desktop.
Save EncodeTheCode/a9915d835dc202af1becde4e35d13aff to your computer and use it in GitHub Desktop.
const BUTTONS = {
'TRIANGLE': 0x1000,
'CIRCLE': 0x2000,
'CROSS': 0x4000,
'SQUARE': 0x8000
};
function isButtonPressed(state, button) {
return (state.buttons & button) === 0;
}
function readGamepadState(gamepad) {
if (!gamepad) return;
const state = {
buttons: gamepad.buttons,
};
console.log("Triangle pressed: ", isButtonPressed(state, BUTTONS['TRIANGLE']));
console.log("Circle pressed: ", isButtonPressed(state, BUTTONS['CIRCLE']));
console.log("Cross pressed: ", isButtonPressed(state, BUTTONS['CROSS']));
console.log("Square pressed: ", isButtonPressed(state, BUTTONS['SQUARE']));
}
function updateGamepads() {
const gamepads = navigator.getGamepads();
for (const gamepad of gamepads) {
if (gamepad) {
readGamepadState(gamepad);
}
}
}
// Poll for gamepad state every 100ms
setInterval(updateGamepads, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment