KeyEvent key Property - Javascript DOM

Javascript examples for DOM:Key Event

Description

The key property returns the identifier of the key pressed.

This property is read-only.

Return Value

A String, representing the pressed keyboard button.

The return value of this property can be a string of:

  • A single character, like "a", "+" or "$"
  • A multicharacter, like "F1", "Enter", "HOME"

The following code shows how to get the key pressed.

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) {/*www  .  java 2  s  . co  m*/
    var x = event.keyCode;               
    var y = String.fromCharCode(x);      
    document.getElementById("demo").innerHTML = "Number: " + x + " = Character: " + y;
}
</script>

</body>
</html>

Related Tutorials