Alert some text if the user presses the Escape key:
Press the Escape/Esc key on the keyboard in the input field.
<!DOCTYPE html> <html> <body> <input type="text" size="50" onkeydown="myFunction(event)"> <p id="demo"></p> <script> function myFunction(event) {//from w w w . j av a2 s . co m var x = event.which || event.keyCode; if (x == 27) { document.getElementById("demo").innerHTML = "You pressed the Escape key!"; } } </script> </body> </html>