I've been trying to make a simpler way to check for keypresses for my game. I really thought this would work. I think it's to do with the event listener. Just wondering why its not working. And is there a more efficent way to do this?
Code: `
const Key = {
isDown: function(name){
document.addEventListener('keypress', function(event) {
if(name == event.code){
return true;
} else {
return false;
};
});
}
};
function checkKeys(){
if(Key.isDown('Space')){
console.log('true')
};
};
function animate() {
requestAnimationFrame(animate);
checkKeys();
};
`
For some reason Key.isDown(KEY)
is not returning true, but it is returning false.