KeyEvent keyCode Property - Javascript DOM

Javascript examples for DOM:Key Event

Description

The keyCode property returns the Unicode character code of the key in the onkeypress event

The keyCode property returns the Unicode key code in onkeydown or onkeyup event.

Character codes is a number which represents an ASCII character

Key codes is a number which represents an actual key on the keyboard

This property is read-only.

Return Value

A Number, representing either a Unicode character code or the Unicode key 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.  j  a  va2  s  .  c o m
    var x = event.keyCode;              
    var y = String.fromCharCode(x);     
    document.getElementById("demo").innerHTML = "Number: " + x + " = Character: " + y;
}
</script>

</body>
</html>

Related Tutorials