Javascript DOM KeyboardEvent charCode Property compare with keyCode

Introduction

A cross-browser solution to 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 a  v a2 s .  c  o m*/
  // Use charCode if the browser supports it, otherwise use event.keyCode for IE8 and earlier
  var x = event.charCode || event.keyCode;
  document.getElementById("demo").innerHTML = "The Unicode value is: " + x;
}
</script>

</body>
</html>



PreviousNext

Related