qadenza
qadenza

Reputation: 9301

prevent Save dialog to appear on Ctrl S

I want to save page changes when Ctrl-S or Ctrl-Enter is pressed

Ctrl-Enter works fine but on Ctrl-S I cannot prevent a Save dialog to appear.

$(document).on('keydown', function(e){    
    if (e.ctrlKey && (e.keyCode == 13 || e.keyCOde == 83)){
        e.preventDefault();
        // save data...
    }
});

Any help?

Upvotes: 0

Views: 1725

Answers (2)

Bilal Ahmed
Bilal Ahmed

Reputation: 1129

This is what I use :

$(document).keydown(function(event) {
    if (!((String.fromCharCode(event.which).toLowerCase() == 's' || event.keyCode == 13) && event.ctrlKey) && !(event.which == 19)) return true;
    alert("Ctrl-S pressed");
    event.preventDefault();
    return false;
});

Another choice is that you can use Shortcut library, you can enjoy more shortcut keys than just ctrl+s. Plus, this library has short & handy code as well :

shortcut.add("Ctrl+S",function() {
    alert("Hi there!");
});

Upvotes: 1

Venom
Venom

Reputation: 1106

Typo in your code e.keyCOde == 83 ===> e.keyCode == 83 [Character "O" should be small]

Upvotes: 1

Related Questions