Skip to content

Instantly share code, notes, and snippets.

@karanjthakkar
Created November 9, 2016 04:53
Show Gist options
  • Save karanjthakkar/9a5df91c1726ba53896228f51185740e to your computer and use it in GitHub Desktop.
Save karanjthakkar/9a5df91c1726ba53896228f51185740e to your computer and use it in GitHub Desktop.
Khalid keystate example
/*eslint fp/no-unused-expression: 0, fp/no-nil: 0 */
const Keystate = {
rightPressedKey: false,
leftPressedKey: false,
spacePressedKey: false,
rPressedKey: false,
addListeners: function() {
document.addEventListener("keyup", e => {
if (e.keyCode === 37) {
Keystate.leftPressedKey = false;
} else if (e.keyCode === 39) {
Keystate.rightPressedKey = false;
} else if (e.keyCode === 32) {
Keystate.spacePressedKey = false;
} else if (e.keyCode === 82) {
Keystate.rPressedKey = false;
}
});
document.addEventListener('keydown', e => {
if (e.keyCode === 37) {
Keystate.leftPressedKey = true;
} else if (e.keyCode === 39) {
Keystate.rightPressedKey = true;
} else if (e.keyCode === 32) {
Keystate.spacePressedKey = true;
} else if (e.keyCode === 82) {
Keystate.rPressedKey = true;
}
});
}
};
export default Keystate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment