Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect escape key press with pure JS or jQuery?

Possible Duplicate:
Which keycode for escape key with jQuery

How to detect escape key press in IE, Firefox and Chrome? Below code works in IE and alerts 27, but in Firefox it alerts 0

$('body').keypress(function(e){     alert(e.which);     if(e.which == 27){         // Close my modal window     } }); 
like image 316
Mithun Sreedharan Avatar asked Jul 30 '10 07:07

Mithun Sreedharan


People also ask

How do you detect when a keyboard key has been pressed using jQuery?

jQuery | keypress() The keypress() method in jQuery triggers the keypress event whenever browser registers a keyboard input. So, Using keypress() method it can be detected if any key is pressed or not.

How do you detect if a key is being pressed in JavaScript?

Using JavaScript In plain JavaScript, you can use the EventTarget. addEventListener() method to listen for keyup event. When it occurs, check the keyCode 's value to see if an Enter key is pressed.

How do you press Esc key?

1. Short for Escape, Esc is a key found on the top-left corner of a computer keyboard. It allows the user to abort, cancel, or close an operation. For example, if a web page was loading slow on an Internet browser, pressing the Escape key stops the download.


2 Answers

Note: keyCode is becoming deprecated, use key instead.

function keyPress (e) {     if(e.key === "Escape") {         // write your logic here.     } } 

Code Snippet:

var msg = document.getElementById('state-msg');  document.body.addEventListener('keypress', function(e) {   if (e.key == "Escape") {     msg.textContent += 'Escape pressed:'   } });
Press ESC key <span id="state-msg"></span>

keyCode is becoming deprecated

It seems keydown and keyup work, even though keypress may not


$(document).keyup(function(e) {      if (e.key === "Escape") { // escape key maps to keycode `27`         // <DO YOUR WORK HERE>     } }); 

Which keycode for escape key with jQuery

like image 135
jmj Avatar answered Sep 20 '22 19:09

jmj


The keydown event will work fine for Escape and has the benefit of allowing you to use keyCode in all browsers. Also, you need to attach the listener to document rather than the body.

Update May 2016

keyCode is now in the process of being deprecated and most modern browsers offer the key property now, although you'll still need a fallback for decent browser support for now (at time of writing the current releases of Chrome and Safari don't support it).

Update September 2018 evt.key is now supported by all modern browsers.

document.onkeydown = function(evt) {      evt = evt || window.event;      var isEscape = false;      if ("key" in evt) {          isEscape = (evt.key === "Escape" || evt.key === "Esc");      } else {          isEscape = (evt.keyCode === 27);      }      if (isEscape) {          alert("Escape");      }  };
Click me then press the Escape key
like image 30
Tim Down Avatar answered Sep 20 '22 19:09

Tim Down