Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key events are not raised when escape key is pressed on a focused element

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.

like image 626
Gqqnbig Avatar asked Feb 12 '16 00:02

Gqqnbig


People also ask

Which event gets activated when a key is pressed?

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.

Which key event does not fire if the key pressed was a modifier key?

So modifier keys like Shift , Control , Alt/Meta , function keys, etc won't fire keypress events.

What is the keycode for Escape key?

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 + [ ).


1 Answers

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

like image 191
rikkertkoppes Avatar answered Nov 15 '22 13:11

rikkertkoppes