When using the example code from the jQuery documentation for the keypress event handler, I'm unable to capture the Delete key. The snippet below is going to log 0
when the Delete key is pressed in FireFox:
$(document).keypress(function(e) {
console.log(e.which);
});
Seems there's gotta be a way to capture the Delete key, but it's an ambiguous term so Google isn't proving to be much help with it.
More recent and much cleaner: use event.key
. No more arbitrary number codes!
NOTE: The old properties (
.keyCode
and.which
) are Deprecated.
document.addEventListener('keydown', function(event) {
const key = event.key; // const {key} = event; ES6+
if (key === "Delete") {
// Do things
}
});
Javascript Keycodes
Except this detail Colin & Tod's answer is working.
$('html').keyup(function(e){
if(e.keyCode == 46) {
alert('Delete key released');
}
});
Source: javascript char codes key codes from www.cambiaresearch.com
Source: Stackoverflow.com