js keydown keys code example

Example 1: javascript keydown

document.addEventListener("keydown", function(event) {
  if (event.keyCode == 32) {
    console.log("Space was pressed!");
  }
});

Example 2: JAVASCRIPT KEYDOWN AROW

document.onkeydown = checkKey;

function checkKey(e) {

    e = e || window.event;

    if (e.keyCode == '38') {
        // up arrow
    }
    else if (e.keyCode == '40') {
        // down arrow
    }
    else if (e.keyCode == '37') {
       // left arrow
    }
    else if (e.keyCode == '39') {
       // right arrow
    }

}