Created
December 10, 2020 09:49
-
-
Save rubenhorn/e0faf9a937ac40eb6d1e9bbe1d88fda1 to your computer and use it in GitHub Desktop.
Function for reading input from Retrolink NES controller (https://www.gadgetreview.com/retrolink-usb-nes-controller-review) in the browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pollNESController() { | |
const gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads : []); | |
const controllerInput = { | |
isPresent: false, | |
dpad: '', | |
select: 0, | |
start: 0, | |
a: 0, | |
b: 0 | |
}; | |
const isPresent = gamepads.length > 0 && gamepads[0] != null; | |
const getButton = (index) => { | |
console.log('test'); | |
if(!isPresent) { | |
return false; | |
} | |
return gamepads[0].buttons[index].pressed; | |
}; | |
controllerInput.isPresent = isPresent; | |
if(getButton(14)) { | |
controllerInput.dpad = 'left'; | |
} | |
else if(getButton(15)) { | |
controllerInput.dpad = 'right'; | |
} | |
else if(getButton(12)) { | |
controllerInput.dpad = 'up'; | |
} | |
else if(getButton(13)) { | |
controllerInput.dpad = 'down'; | |
} | |
controllerInput.select = getButton(8); | |
controllerInput.start = getButton(9); | |
controllerInput.a = getButton(1); | |
controllerInput.b = getButton(0); | |
return controllerInput; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment