views:

6160

answers:

7

I have two functions. When enter is pressed the functions runs correctly but when escape is pressed it doesn't. What's the correct number for the escape key?

$(document).keypress(function(e) { 
    if (e.which == 13) { $('.save').click(); }    // enter (works as expected)
    if (e.which == 27) { $('.cancel').click(); }  // esc   (does not work)
});
+1  A: 

27 is the code for the escape key. :)

Salty
I have two functions when enter is press the functions runs correctly but not with escape key.. $(document).keypress(function(e) { if (e.which == 13) { $('.save').click(); } if (e.which == 27) { $('.cancel').click(); } });
Shishant
$(document).keypress(function(e) { y=e.keyCode?e.keyCode:e.which;});When I alert(y), it alerts 27 in IE and FF. Are you sure there's not something else wrong with your code?
Salty
+1  A: 

Have a look here.

kgiannakakis
+15  A: 

Try with the keyup event:

$(document).keyup(function(e) {
  if (e.keyCode == 13) { $('.save').click(); }     // enter
  if (e.keyCode == 27) { $('.cancel').click(); }   // esc
});
CMS
This has nothing to do with keyup vs. keypress. To capture ESCAPE, you need to look at e.keyCode as opposed to e.which (which this example happens to do).
dkamins
"This has nothing to do with keyup vs. keypress" - that's incorrect, dkamins. keypress doesn't seem to be handled consistently between browsers (try out the demo at http://api.jquery.com/keypress/ in IE vs Chrome vs Firefox -- sometimes it doesn't register, and both 'which' and 'keyCode' vary) whereas keyup is consistent. e.which is the jquery-normalized value, so 'which' or 'keyCode' should both work in keyup.
Jordan Brough
+2  A: 

To get the hex code for all the characters: http://asciitable.com/

Julien
I was gonna say..... I hate that this site is at the top of the search results because it's so damn ugly, but it does convey the necessary information.
Mark
also can use 'man ascii' in *nix
gacrux
A: 

Rather than hardcode the keycode values in your function, consider using named constants to better convey your meaning:

var KEYCODE_ENTER = 13;
var KEYCODE_ESC = 27;

$(document).keyup(function(e) {
  if (e.keyCode == KEYCODE_ENTER) { $('.save').click(); } 
  if (e.keyCode == KEYCODE_ESC) { $('.cancel').click(); } 
});

Some browsers (like FireFox, unsure of others) define a global KeyEvent object that exposes these types of constants for you. This SO question shows a nice way of defining that object in other browsers as well.

Seth Petry-Johnson
A: 

To capture ESCAPE, you need to look at e.keyCode as opposed to e.which. The value will be 27.

dkamins
A: 

Your code works just fine. It's most likely the window thats not focused. I use a similar function to close iframe boxes etc.

$(document).ready(function(){

    // Set focus
    setTimeout('window.focus()',1000);

});

$(document).keypress(function(e) {

    // Enable esc
    if (e.keyCode == 27) {
      parent.document.getElementById('iframediv').style.display='none';
      parent.document.getElementById('iframe').src='/views/view.empty.black.html';
    }

});
Trikks