问题
According to some questions I find can out how to prevent a key combination, but I want to define a short key...
One way is to use bind() function in jQuery, and the problem is, keys default job is still working. i.e: Ctrl+s = savepage
Any solution?
回答1:
The question you linked isn't exactly what you were looking for, but the same concept can be used. Here's the jQuery equivalent only listening for Ctrl+s
$(document).on("keydown",function(e){
if (e.originalEvent.ctrlKey && e.which == 83/*"s"*/) {
setTimeout(function(){ //Fix for firefox. Ref: http://stackoverflow.com/questions/14860759/cant-override-ctrls-in-firefox-using-jquery-hotkeys
// do save stuff...
alert("Saved!"); // if you remove alert, you can remove setTimeout
},0);
return false;
}
});
http://jsfiddle.net/SEQVD/3/
Tested in Chrome, Firefox, and IE.
回答2:
Please ensure that the events you've bind to a shortcut contain a preventDefault. In jquery this should look like:
function event(e) {
e.preventDefault();
}
Using other frameworks this could be different like e.stop() or e.stopEvent().
来源:https://stackoverflow.com/questions/15205150/how-to-replace-browser-short-keys-defaults