Detect delete key on the entire page with jQuery
I need to be able to detec开发者_运维知识库t a delete key over any portion of the page. I have this code...
$(document).keydown(function (e) {
console.log(e);
});
but I can't seem to get the event to fire at all..
You should check on key up (if possible).
$(document).keyup(function (e) {
if (e.keyCode == 46) {
// Do it
}
});
As for the event handler not working, it does when I try it, is there anything that is stopping it from being fired? JavaScript error? Something stopping the event from bubbling up (such as e.stopPropagation()
)?
Found this Capturing "Delete" Keypress with jQuery
console.log(e.which);
This worked for me:
$(document).keydown(function(e) {
console.log(e.which);
});
Delete key is 46 as per console.log
$(document).bind('keydown',function(e){
console.log(e);
var keyCode = e.keyCode || e.which;
if(keyCode == 46){
//delet key pressed
}
});
精彩评论