3

I have a page on which I want to override default ctrl+s behavior. When typing this on google or stack-overflow, I can only find ways to do it with jquery:

$(window).keypress(function(event) {
    if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
    alert("Ctrl-S pressed");
    event.preventDefault();
    return false;
});

I'd like to have a clean way to do it with vanilla JS.

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82

1 Answers1

3

Not a big difference from the jQuery implementation:

window.addEventListener("keypress", function(event) {
  if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true
  alert("Ctrl-S pressed")
  event.preventDefault()
  return false
})
lukaleli
  • 3,427
  • 3
  • 22
  • 32