Can't fire keypress event when press delete key
Asked Answered
H

2

21

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);
Hannibal answered 17/4, 2012 at 8:6 Comment(0)
I
26

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);
Insulator answered 17/4, 2012 at 8:29 Comment(0)
R
5

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

Rabon answered 29/11, 2012 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.