Javascript / JQuery: Detecting the delete key

Recently I was using JQuery to detect when the delete key was pressed. I was having some issues getting it to work. I was listening to the “keypress” event, but the delete key was never detected.

$('html').keypress(function(e){
    if(e.keyCode == 46) {
        alert('Delete key pressed');
    }
});

After a bit of research I realized that “keyup” made more sense. “keypress” is for printable characters. “keydown” and “keyup” will capture all characters

$('html').keyup(function(e){
    if(e.keyCode == 46) {
        alert('Delete key pressed');
    }
});

I hope this helps someone else down the road that may be stuck on this.