+ 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?
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
+ 4
https://code.sololearn.com/WiBmEEtaNcxk/?ref=app