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
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