0

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?

Adriano
  • 3,788
  • 5
  • 32
  • 53
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • Possible duplicate of [How to disable save as dialog in Chrome?](https://stackoverflow.com/questions/28710702/how-to-disable-save-as-dialog-in-chrome) – Alexander Nied Oct 17 '19 at 02:14

2 Answers2

1

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

Venom
  • 1,076
  • 1
  • 11
  • 23
0

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!");
});
Bilal Ahmed
  • 1,043
  • 4
  • 17
  • 32