javascriptgoogle-chrome

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.


Solution

  • 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