Sometimes, we want to capture a CTRL-S press with JavaScript.
In this article, we’ll look at how to capture a CTRL-S press with JavaScript.
How to capture a CTRL-S press with JavaScript?
To capture a CTRL-S press with JavaScript, we can listen to the keydown event.
For instance, we write:
document.onkeydown = (e) => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
console.log('CTRL + S');
}
}
to set the document.onkeydown
property to a function that checks if ctrl+s is pressed with e.ctrlKey && e.key === 's'
.
If it’s true
, we call e.preventDefault
to stop the save file dialog from opening.
And then we log 'CTRL + S'
.
Conclusion
To capture a CTRL-S press with JavaScript, we can listen to the keydown event.