Javascript DOM KeyboardEvent charCode Property

Introduction

Get the Unicode value of the pressed keyboard key:

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" size="40" onkeypress="myFunction(event)">
<p id="demo"></p>
<script>
function myFunction(event) {/*w  w w . j av  a 2 s.  c o  m*/
  var x = event.charCode;
  document.getElementById("demo").innerHTML = "The Unicode value is: " + x;
}
</script>

</body>
</html>

The charCode property returns the Unicode character code of the key that triggered the onkeypress event.

To convert the Unicode value into a character, use the fromCharCode() method.

If this property is used on onkeydown or onkeyup events, the returned value is always "0".

This property is read-only.




PreviousNext

Related