The Gamer King The Gamer King - 7 months ago 27
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')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download