Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
How to create an event listener for the keyboard event 'ctrl + q' without it closing the browser window?

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;
    
  }
  
});
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!