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

362
Visualizações
How can I hit multiple html buttons at once in javascript?

I can click the arrow buttons on the web page, but multiple buttons cannot be read at the same time.If I click on 37 and 39 keys it does not work but only when I click on 37 keys it works. Same goes for other keys, how can I control more than one button at the same time?

const handleArrowButtonMouseDown = function() {
  $('button').on('mousedown touchstart', (e) => {
    e.preventDefault();
    if(state.arrowPressed) return;
    let direction = $(e.target).closest('button').attr('id');
    state.setArrowPressed(direction);
    state.setRobotCommand();
  });
};

const handleArrowButtonMouseUp = function() {
  $('button').on('mouseup mouseleave touchend', endCurrentOperation);
};


const handleArrowKeyDown = function() {
  $(document).keydown((e) => {
    if(state.arrowPressed) return;
    switch(e.which) {
      
    case 37: 
      state.setArrowPressed('left');
      break;

    case 38: 
      state.setArrowPressed('up');
      break;

    case 39: 
      state.setArrowPressed('right');
      break;

    case 40: // down
      state.setArrowPressed('down');
      break;

    default: return; 
    }
    e.preventDefault(); 
    state.setRobotCommand();
  });
};

const handleArrowKeyUp = function() {
  $(document).keyup((e) => {
    if (e.which === 37 && state.arrowPressed !== 'left') return;
    if (e.which === 38 && state.arrowPressed !== 'up') return;
    if (e.which === 39 && state.arrowPressed !== 'right') return;
    if (e.which === 40 && state.arrowPressed !== 'down') return;  
    endCurrentOperation();
  });
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is one way, but it's not safe to rely on, since you cannot detect if a key is currently down in JavaScript.

The example below works only with Shift + Number. It's closest to achieve when both keys are pressed.

window.addEventListener("keydown", function (event) {
     if (event.shiftKey) {
          if (/\d/.test(event.code)){
               console.log("SHIFTKEY + Number:"+event.code);
         }  
     }
}, true)

Maybe you can repeat the same login to your code.

about 4 years ago · Juan Pablo Isaza Relatório
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