KeyEvent charCode Property - Javascript DOM

Javascript examples for DOM:Key Event

Description

The charCode property returns the key code which triggered the onkeypress event.

In onkeydown or onkeyup events, the returned value is always "0".

This property is read-only.

Return Value

A Number, representing the Unicode character code

The following code shows how to get the Unicode value of the pressed keyboard key:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

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

<p id="demo"></p>

<script>
function myFunction(event) {//from w  w w .  ja va  2s  .c o  m
    var x = event.charCode || event.keyCode;  // Get the Unicode value
    var y = String.fromCharCode(x);           // Convert the value into a character
    document.getElementById("demo").innerHTML = "Number: " + x + " = Character: " + y;
}
</script>

</body>
</html>

Related Tutorials