"keyCode" function gets slashed out on writing it in VS code

Viewed 810

Here's the fragment of the js code for your reference. At the place where I have written e.keyCode, the text keyCode gets slashed out and a yellow bulb appears alongside it.

document.onkeydown = function(e) {
    console.log("Key code is: ", e.keyCode)
}
3 Answers

use KeyboardEvent.code instead.

Check it out at the MDN docs here

You can play with my code snippet and get info from console about event-codes(just press different buttons and see the result)

window.addEventListener("keydown", function(event) {
  let str = "KeyboardEvent: key='" 
    + event.key + "' | code='" 
    + event.code + "'"; 
  console.log(str);
}, true);

Solved this problem by adding event listener in the beginning. Here's the fragment of code for your reference.

document.addEventListener('keydown', function(e){
    if(e.keyCode==32){
        hero = document.querySelector('.hero');
        hero.classList.add('animatehero');
        audiojump.play();
}
Related