how do i listen to a keypress in javascript 1

how do i listen to a keypress in javascript

let b = document.getElementById("body"); //making var for body
b.addEventListener("keydown", (evt) => {//when this happens
	console.log(evt.keyCode); //log keycode
});

Here is what the above code is Doing:
1. We’re creating a variable called b that is equal to the body element.
2. We’re adding an event listener to the body element that listens for a keydown event.
3. When the keydown event happens, we’re logging the keycode of the key that was pressed.

Now, let’s add some code to make the player move.

Similar Posts