15

Using one of the examples from a previous question I have:

$(window).keypress(function(event) {
    if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
    $("form input[name=save]").click();
    event.preventDefault();
    return false;
});

Is it also possible to change this to work for the Mac cmd key?

I have tried (!(event.which == 115 && (event.cmdKey || event.ctrlKey)) && !(event.which == 19)) but this didn't work.

Barlas Apaydin
  • 7,233
  • 11
  • 55
  • 86
John Magnolia
  • 16,769
  • 36
  • 159
  • 270
  • 1
    possible duplicate of [How can I capture COMMAND+S in jQuery?](http://stackoverflow.com/questions/5833325/how-can-i-capture-commands-in-jquery) `:)` – Tats_innit Jul 16 '12 at 10:03
  • Key-Events are pretty inconsistent between browsers and OS. Just code an small example and check which keys and modifiers are triggered when pressing cmd+S. – Christoph Jul 16 '12 at 10:17
  • I can't say this for sure but as a general rule I think you want to avoid changing the default behaviors of the browser (ctrl+s/cmd+s === "Save Page As") – jrz Dec 18 '14 at 18:34

3 Answers3

26

Use the event.metaKey to detect the Command key

$(document).keypress(function(event) {
    if (event.which == 115 && (event.ctrlKey||event.metaKey)|| (event.which == 19)) {
        event.preventDefault();
        // do stuff
        return false;
    }
    return true;
});
Anil
  • 21,730
  • 9
  • 73
  • 100
spyke
  • 276
  • 3
  • 3
  • 3
    Typo missing bracket. I tried to edit this so but get error: "Edits must be at least 6 characters; is there something else to improve in this post?" stupid stackoverflow – John Magnolia Oct 12 '12 at 08:17
  • 6
    unfortunately this does not work for me in chrome(Version 27.0.1453.110). it always opens the "save as" dialog... - CTRL + S works on the other hand – m3o Jun 10 '13 at 23:14
  • The answer is good but the question is bad - we shouldn't generally override default behaviors in browsers (meta+s === "save"). Saving a webpage isn't a particularly useful thing to do, but it's best to not pointlessly defy user expectations. – jrz Dec 18 '14 at 18:25
  • 1
    Jon z, what if you want to paste text inside a canvas element? It's very useful. – Chewie The Chorkie Jul 10 '17 at 19:34
4

For detecting ctrl+s and cmd+s, you can use this way:

Working jsFiddle.

jQuery:

var isCtrl = false;
$(document).keyup(function (e) {
 if(e.which == 17) isCtrl=false;
}).keydown(function (e) {
    if(e.which == 17) isCtrl=true;
    if(e.which == 83 && isCtrl == true) {
        alert('you pressed ctrl+s');
    return false;
 }
});

source (includes all keyboard shorcuts and buttons)

Barlas Apaydin
  • 7,233
  • 11
  • 55
  • 86
  • Nice, can you explain why this is a better solution than the other answer? – John Magnolia Jul 19 '13 at 07:45
  • 1
    On my Macbook ( Chrome and Safari ) cmd+s doesn't work until you press ctr+s. Then both work. Weird. – T. Brian Jones Jul 22 '13 at 16:40
  • @T.BrianJones same thing happend to me on different browsers. I guess browser's `save` function prevents our function /: – Barlas Apaydin Jul 22 '13 at 21:18
  • @BarlasApaydin - DannyR's answer worked for my application in this question: http://stackoverflow.com/questions/93695/best-cross-browser-method-to-capture-ctrls-with-jquery/14180949#comment25956719_14180949 – T. Brian Jones Jul 22 '13 at 21:59
2

This works for me:

$(document).keypress(function(event) {
    if ((event.which == 115 || event.which == 83) && (event.ctrlKey||event.metaKey)|| (event.which == 19)) {
        event.preventDefault();
        // do stuff
        return false;
    }
    return true;
});
Michael_Scharf
  • 33,154
  • 22
  • 74
  • 95