Skip to content

JavaScript onkeyup keycode | get the key code example

  • by

Is it possible to get the key code from onkeyup?

Yes, you can by using a function in the element onkeyup property. And pass event to the function from which you can access the property keyCode.

The keyup events provide a code indicating which key is pressed

JavaScript onkeyup keycode example

HTML example code. Alert text if the user presses any key. In alert test will show the key pressed and its key code.

<!DOCTYPE html>
<html>
<head>
  <script>
    function myFunction(event) {
      var x = event.keyCode;

      alert(event.code + " " + x);

    }
  </script>
</head>

<body>
  <input type="text" onkeydown="myFunction(event)">

</body>
</html>

Output:

JavaScript onkeyup keycode

Documentation: KeyboardEvent.keyCode

Do comment if you have any suggestions or doubts 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.

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