Hey, please help me with a JavaScript thing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Hey, please help me with a JavaScript thing

I'm making a 2d game using HTML, CSS and JavaScript. I need to make the character move left and right using the keyboard (keydown, keyup). Can somebody send me an example, please?

13th Mar 2019, 1:24 AM
I_am_L
I_am_L - avatar
2 Answers
+ 6
All you need is to add eventListeners on the elements or may be in the entire document, depending upon the need. A simple example would tell you how to get keyCode and keyName so that you can specify their work. document.addEventListener('keydown', (event) => { const keyName = event.key; const keyCode= event.which; //some other stuffs }) ; Similarly can be done for keyPress and keyUp. Consider reading more at: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
13th Mar 2019, 1:38 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 4
https://code.sololearn.com/WiBmEEtaNcxk/?ref=app
13th Mar 2019, 3:49 AM
Calviղ
Calviղ - avatar