2

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pooya
  • 1,508
  • 3
  • 17
  • 38

2 Answers2

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.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • this is what I want... is there any tip for using `e.originalEvent.ctrlKey` Instead of ctrl+s – Pooya Mar 06 '13 at 14:04
0

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().

mr.VVoo
  • 2,265
  • 20
  • 30