Skip to content

charcode in JavaScript | Get Unicode character of the key on the keyboard

  • by

e.charCode – a number that represents the Unicode character of the key on the keyboard. The charCode is a property, which returns the Unicode character code of the key that triggered the onkeypress event.

Example charcode in JavaScript

Press a key on the keyboard in the input field to get the Unicode character code of the pressed key.

<!DOCTYPE html>
<html>
<body>

  <input type="text" size="40" onkeypress="myFunction(event)">

  <p id="res"></p>


  <script>
    function myFunction(event) {
      var x = event.charCode;
      document.getElementById("res").innerHTML = x;
    }
  </script>

</body>
</html>

Output

charcode in JavaScript  Unicode character key keyboard

Do comment if you have any doubts or suggestions on this JS code.

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