How do I capture a CTRL-S without jQuery or any other library?

An up to date answer in 2020.

Since the Keyboard event object has been changed lately, and many of its old properties are now deprecated, here’s a modernized code:

document.addEventListener('keydown', e => {
  if (e.ctrlKey && e.key === 's') {
    // Prevent the Save dialog to open
    e.preventDefault();
    // Place your code here
    console.log('CTRL + S');
  }
});

Notice the new key property, which contains the information about the stroked key. Additionally, some browsers might not allow code to override the system shortcuts.

Leave a Comment