Javascript DOM KeyboardEvent keyCode Property get escape key

Introduction

Alert some text if the user presses the Escape key:

Press the Escape/Esc key on the keyboard in the input field.

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" size="50" onkeydown="keyCode(event)">
<p id="demo"></p>
<script>
function keyCode(event) {//from w  w w  . j a va2  s.  c  o  m
  var x = event.keyCode;
  if (x == 27) {
    document.getElementById("demo").innerHTML = "You pressed the Escape key!";
  }
}
</script>

</body>
</html>



PreviousNext

Related