0

I'm trying to do a shortcut.

When the escape is clicked, should appear a message, but is not working.

I tried with the enter(13), and it worked:

JSFiddle:

http://jsfiddle.net/ENK9p/

function shortcuts(event) {
            if (event.which === 27) {
            alert('teste');
                event.preventDefault();
                return false;
            }
        };

<textarea name='static' id='static' onkeypress="shortcuts(event);"></textarea>

3 Answers3

2

You need to use keydown event. As you have tagged jQuery, You should bind event using it.

$('#static').on('keydown', function (event) {
    if (event.which === 27) {
        alert('teste');
        event.preventDefault();
        return false;
    }
});

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
0

If you want to use the jquery method, use Satpal's method. If you want to use plain javascript, you should change the event to onkeydown and make sure your function is defined in the head. In the fiddle you linked, your javascript is all wrapped in onDomReady, so it's not defined when the keydown event occurs.

Fiddle

xdumaine
  • 10,096
  • 6
  • 62
  • 103
0

Here is example: http://jsfiddle.net/ENK9p/1/

You can use keyup() methoed to detect ESC key press

$('input').keyup(function(e){
    if(e.keyCode == 27){
       alert("test");
    }
});
OzerGul
  • 19
  • 10