Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

keypress - Invoke Escape key press event by jquery

I want to make my question clear. Please dont confuse it with determining the key press. I dont want the user to press any key. On the contrary, I want to run a script which fires the Escape key press event. Please help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use $.Event() to create an Event object containing a keydown event with the keyCode of 27 (which should be Esc) like:

var esc = $.Event("keydown", { keyCode: 27 });
$("body").trigger(esc); // change body to the element where you'd like to execute the escape key press.

See this working demo and the docs at jquery.com

Also note that, as stated by Christofer, this might not be working in all browsers and environments as it might be considered shady behavior that could be used in a malicious manner - just like triggering a click event on an <a> with an external href will not happen. I successfully tested this (the Esc) in Chrome, Safari, Opera, FF & IE9 though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...