14

Briefly, my problem is that I would like to execute the effect of Esc button by a line of Javascript, for example execute an effect onclick of the Esc button. How can I do It ?

[ What I have is an upload in a jQuery box, so I would like that when the upload will finish, the escape function will be executed automatically to close the box]

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hamza
  • 241
  • 2
  • 3
  • 8
  • 1
    Isn't there a method that you can call to close the dialog? – Ruan Mendes Jun 21 '11 at 22:56
  • @HAMZA Could you please let us know how the selected answer has helped? To me the question definitely is about `emulating a key stroke` (`esc` key in particular), whereas the answer by @Nathan is to **capture** the `esc` key, thus requiring the user to manually type it. The accepted answer should have been the one by @Juan. Either that, or OP has totally asked the wrong question! – bPratik Apr 27 '12 at 15:30
  • @bPratik If you see my comment under the accepted answer, that answer probably wouldn't work, because it relies on other code listening for the `escapeKeyPressed` event. I believe the OP just wanted to know how to close the dialog, and Nathan's comment (`$('#myDialog').trigger('dialogClose')`) showed the OP how to do it. – Ruan Mendes Apr 27 '12 at 15:35
  • This library claims to be able to fire key events. http://jupiterjs.com/news/syn-a-standalone-synthetic-event-library It says you should be able to just call: `Syn.type('[escape]');` – Ruan Mendes Jun 20 '11 at 22:13
  • Ah, I suppose... but I still believe in the context of the question, your answer should be the accepted answer for this question, unless the OP edits the question!! Or at least, yours should be voted more than the accepted one as it answers the question! – bPratik Apr 27 '12 at 15:46
  • @bPratik: And now some moderator decided that my answer was not an answer an converted it to a comment. Without an explanation! What a jerk. – Ruan Mendes Apr 28 '12 at 00:38
  • Very weird... although the mod also unaccepted @Nathan's answer. – bPratik Apr 28 '12 at 00:43
  • I believe that your answer was converted because technically it is not solving the issue and is more of a suggestion towards the right direction. Just my assumption though! – bPratik Apr 28 '12 at 00:45

2 Answers2

19

I know it's an old question. However if somebody lands in here on search, it may help:

First, trigger the event on desired element (or document),

$('a[name=close]').click(function(){
    var e = jQuery.Event("keyup"); // or keypress/keydown
    e.keyCode = 27; // for Esc
    $(document).trigger(e); // trigger it on document
});

And then, have a keyup listener on document:

// Esc key action
$(document).keyup(function(e) {
    if (e.keyCode == 27) { // Esc
        window.close(); // or whatever you want
    }
});
pMan
  • 8,808
  • 11
  • 32
  • 35
2

This code works for me

$(document).keyup(function(e) {     
    if(e.keyCode== 27) {
        alert('Esc key is press');  
    } 
});
binarious
  • 4,568
  • 26
  • 35
manish
  • 47
  • 1