Javascript DOM KeyboardEvent charCode Property check key code and char code

Introduction

Alert some text if the user presses the "O" key:

Press the "O" key on the keyboard in the input field to alert some text.

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" size="40" onkeypress="myFunction(event)">
<p id="demo"></p>
<script>
function myFunction(event) {//from   www .ja  v a 2 s  .  co  m
  var x = event.charCode || event.keyCode;
  if (x == 111 || x == 79) {  // o is 111, O is 79
    document.getElementById("demo").innerHTML = "You pressed the 'O' key!";
  }
}
</script>

</body>
</html>



PreviousNext

Related