51

I Want to catch Ctrl+S in Chrome, and prevent the default browser behavior to save the page. How?

(Just posting the question & answer as I was after this for a pretty long time and didn't find a solution)

1

4 Answers 4

63

As far as I can see, the secret sauce is, that Ctrl+S does NOT fire the keypress event, only the keydown event.

Using jQuery.hotkeys:

$(document).bind('keydown', 'ctrl+s', function(e) {
    e.preventDefault();
    alert('Ctrl+S');
    return false;
});

Only with jQuery:

$(document).bind('keydown', function(e) {
  if(e.ctrlKey && (e.which == 83)) {
    e.preventDefault();
    alert('Ctrl+S');
    return false;
  }
});

Edit 2012.12.17 - jQuery.hotkeys says

Hotkeys aren't tracked if you're inside of an input element (unless you explicitly bind the hotkey directly to the input). This helps to avoid conflict with normal user typing.

4
  • 3
    jQuery.hotkeys does not seem to capture the event when a textarea or input field is focused. Jul 6, 2012 at 19:14
  • @BrianM.Hunt very true thanks. Quoted docs as this is a documented "feature"
    – zupa
    Dec 17, 2012 at 15:57
  • 1
    Tip: Do not use keyup, this will always trigger the save dialog first. Always go for keydown.
    – Avatar
    Aug 25, 2016 at 14:26
  • Tip: Do not use jQuery, this will only bring sadness.
    – Klesun
    May 18, 2021 at 18:39
31

"Borrowed" from Overriding control+s (save functionality) in browser

document.addEventListener("keydown", function(e) {
  if (e.key === 's' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
    e.preventDefault();
    alert('captured');
  }
}, false);
1
  • 3
    Note, in 2k21 keyCode is deprecated, it's recommended to use keyboardEvent.key === 's' nowadays... Actually, why comment when I can edit >:D
    – Klesun
    May 18, 2021 at 18:41
6

document.onkeydown = function (e) {
    e = e || window.event;//Get event
    if (e.ctrlKey) {
        var c = e.which || e.keyCode;//Get key code
        switch (c) {
            case 83://Block Ctrl+S
                e.preventDefault();     
                e.stopPropagation();
            break;
        }
    }
};
   

1

All in one solution to prevent data

// disable right click
$(function() {
    $(this).bind("contextmenu", function(e) {
        e.preventDefault();
    });
}); 

// Prevent F12      
$(document).keydown(function (event) {
    if (event.keyCode == 123) { // Prevent F12
        return false;
    } else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) { // Prevent Ctrl+Shift+I        
        return false;
    }
});

//stop copy of content
function killCopy(e){
    return false
}
function reEnable(){
    return true
}
document.onselectstart=new Function ("return false")
    if (window.sidebar){
    document.onmousedown=killCopy
    document.onclick=reEnable
}

// prevent ctrl + s
$(document).bind('keydown', function(e) {
  if(e.ctrlKey && (e.which == 83)) {
    e.preventDefault();
    return false;
  }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.