How to detect keypresses in JavaScript?

Spread the love

To detect keypresses in JavaScript, we add a keypress event listener.

For instance, we write

document.onkeypress = (e) => {
  console.log(e.keyCode);
};

to set document.onkeypress to a function that logs the code of the key pressed by logging e.keyCode.

Leave a Reply

Your email address will not be published. Required fields are marked *