Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda