can't fire keypress event when press delete key

qiu8310 picture qiu8310 · Apr 17, 2012 · Viewed 16.1k times · Source

I'm finding that the delete key doesn't fire the keypress event in Chrome, while other keys work. The problem doesn't occur in Firefox, just in chrome, why? Here is my code:

document.addEventListener('keypress', function (e) {
     console.log(e);
}, false);

Answer

Josh Davenport picture Josh Davenport · Apr 17, 2012

Use keydown or keyup instead, it captures the delete key (as well as others that keypress doesn't, see http://www.quirksmode.org/js/keys.html)

document.addEventListener('keydown', function (e) {
     console.log(e);
}, false);