Capture jQuery CTRL + S and Command + S (Mac)

I know that this question was asked by serval times here in stackoverflow, and I tried several different solutions, but cannot find the code that works for me.

I want to grab the keyboard command Ctrl+ Sand Command+ Son a Mac, I am currently using this code (since jQuery 2.1.0):

jQuery(window).on('keypress', function(event){
    if (!(event.which == 115 && (event.ctrlKey||event.metaKey)) && !(event.which == 19)) return true;

    // my save function

    event.preventDefault();
    return false;
});

This code works fine in:

  • Safari: c Ctrl+ Sand Command+S
  • Firefox: c Ctrl+ Sand Command+S
  • Chrome: Only Ctrl + Sworks

You will see that the problem is with Google Chrome, here I can only capture Ctrl+ S.

Does anyone know how I can solve this problem?

+4
1

. Chrome (v33) Firefox (v24) Safari (v6.1.1). Control + S, Command + S.

$(document).keydown(function(event) {
        // If Control or Command key is pressed and the S key is pressed
        // run save function. 83 is the key code for S.
        if((event.ctrlKey || event.metaKey) && event.which == 83) {
            // Save Function
            event.preventDefault();
            return false;
        };
    }
);

, keydown, keypress. jQuery docs :

: keypress , , , , .

.

+11

Source: https://habr.com/ru/post/1526024/


All Articles