The Gamer King The Gamer King - 2 months ago 11
Javascript Question

How To Tell If A Key Is Pressed

So I'm making a game and I want to be able to add controls for a character. Hopefully something simple so kind of like

if(W is pressed){
Do something
}

Answer Source

You can add an EventListener for keydown and check the event :

document.addEventListener('keydown', function(e){
   if(e.key === 'w')
   console.log('hei w')
})