2

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
  • 8,503
  • 5
  • 45
  • 74

1 Answers1

2

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,117
  • 3
  • 20
  • 32