I am completely new to the world of coding and am currently diving into learning javascript. Please excuse any misunderstandings I may have as I navigate this new territory. While I've been successful in grasping the basics through self-teaching, I have encountered a roadblock when attempting to detect multiple keypresses simultaneously. After researching various methods, it seems that the general approach involves tracking key events on 'keydown' and 'keyup'. However, I am struggling with setting keyBoard['w'].pressed = true
.
const keyBoard = {
w: {pressed: false}
}
document.addEventListener('keyDown', (e) => {
keyBoard[e.key].pressed = true
});
if(keyBoard['w'].pressed){
alert("success!")
}
The code above represents a simple test case I created to help me better understand the concept. Despite experimenting extensively, it is evident that the issue lies in accurately setting 'w.pressed' to true. I recognize the necessity of incorporating a 'keyup' listener to reset it to false eventually, but for now, my main focus is on resolving the current challenge. Your assistance is greatly appreciated!