Skip to content

JavaScript keydown keycode | KeyboardEvent Example code

  • by

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 the document and handle the user keydown event.

Detect JavaScript keydown keycode

HTML example code:

The 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 example.

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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading