1document.addEventListener('keydown', function(event) {
2 if(event.keyCode == 37) {
3 alert('Left was pressed');
4 }
5 else if(event.keyCode == 39) {
6 alert('Right was pressed');
7 }
8});
1document.addEventListener("keydown", function(event) {
2 if (event.keyCode == 32) {
3 console.log("Space was pressed!");
4 }
5});
1document.addEventListener("keydown", function(event) {
2 console.log(`Key pressed: ${event.key}`);
3 // event.key is a string representing which key has been pressed
4});