JavaScript keydown keycode | KeyboardEvent Example code

The KeyboardEvent.keyCode property is identifying the value of the pressed key. This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key.

Simple addEventListener on document and handle the user keydown event.

Detect JavaScript keydown keycode

HTML example code:

Output will show the pressed button name with its keycode numeric value.

<!DOCTYPE html>
<html>
<body>

  <script>
    document.addEventListener("keydown", function(e) {
      console.log(e.code, e.which);
    })
  </script>

</body>
</html>

Output:

JavaScript keydown keycode

Do comment if you have any doubts and suggestions on this JS keycode examples.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.