Problem summary: The eventListener for the 'keydown' event is supposed to perform a function when the 'ctrl' + 'q' keys are pressed simultaneously, similar to the keyboard shortcuts for search boxes on many popular websites these days. Of course, when I press both keys on my computer, it tries to close the window.
How do I prevent the operating system's default event for the keyboard shortcut from firing when this website is opened in a tab? Any tips on how I can implement the shortcut functionality of this key combination?
Current code (examplified):
let searchBoxIsOpen = false;
document.addEventListener('keydown', (event) => {
event.stopPropagation();
event.preventDefault();
if (
event.ctrlKey
&& event.keyCode == 81
&& !(event.shiftKey || event.altKey || event.metaKey)
) {
if (!searchBoxIsOpen) {
searchBoxIsOpen = true;
console.log("alert");
return;
}
console.log("delert");
return;
}
});