Overriding control+s (save functionality) in browser

You receive two keydown events: The first is for the control key, and the second is for the letter with the modifier flag turned on. Here’s how you listen for a key with a modifier pressed:

document.addEventListener("keydown", function(e) {
  if (e.keyCode === 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
    e.preventDefault();
    // Process event...
  }
}, false);

keyCode === 83 corresponds to S key press.

Taking a page from Google Docs, it uses Cmd-S on Mac and Ctrl-S on other platforms.

Leave a Comment