简体   繁体   中英

Can't fire keypress event when press delete key

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);

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);

keypress event for (Del, End, Home,etc..) is not fired in IE, Chrome and safari .. it only works in firefox. .

so you can use the keyup or keydown event because the keypress event is intented for real (printable) characters. "keydown" is handled at a lower level so it will capture all non-printing keys like DEL, End, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM