Get keycode JavaScript Property | find the key code for a specific key

  • by

Use event.keyCode to get keycode of pressed keyboard key in JavaScript. The keyCode property returns the Unicode character code of the key that triggered the onkeypress event.

JavaScript get keycode Example

HTML example JavaScript gets keycode from char pressed by the user. When the user pressing the button it will show output into a console.

It’s a simplest way to detect keypresses in JavaScript.

<!DOCTYPE html>
<html>
<body>

  <script>
    document.addEventListener('keypress', function(e) {
      console.log("Key: " + e.code + ", Code: " + e.charCode)
    });
  </script>

</body>
</html>

Output:

Get keycode JavaScript Property

Vanilla JavaScript + Alert:

document.addEventListener('keypress', function(e) {
  alert("Key: " + e.code + ", Code: " + e.charCode)
});

Do comment if you have any doubts and suggestions on this JS Keycode topic.

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

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

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