This code works for me. I tested it in Chrome (v33) Firefox (v24) and Safari (v6.1.1 ). Both Control+S and Command+S work.
$(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;
};
}
);
Please note that I am using keydown
and not keypress
. In the jQuery docs they sate:
Note: as the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.
I would avoid using it.