I want to know when the Esc key is pressed on an input element. On Chrome 47.0.2526.106 m, the Esc key removes the focus, but JavaScript cannot catch the keyup
, keydown
, or keypress
events.
Demo: https://jsfiddle.net/gstvj9uq/
$("input").keydown(function() {
alert('keydown');
});
$("input").keyup(function() {
alert('keyup');
});
$("input").keypress(function() {
alert('keypress');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
Can anyone give a solution to catch Escape key down/up/press event on an input? IE 10 can catch the event, however.
The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.
So modifier keys like Shift , Control , Alt/Meta , function keys, etc won't fire keypress events.
On computer keyboards, the Esc key Esc (named Escape key in the international standard series ISO/IEC 9995) is a key used to generate the escape character (which can be represented as ASCII code 27 in decimal, Unicode U+001B, or Ctrl + [ ).
Had the same problem, turned out to be the vimium extention. Without that, focus is not lost on pressing escape.
Note that the keypress event is never fired for escape, only keydown and keyup
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With