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

197
Visualizações
How to respond to multiple keys being pressed at the same time

Basically, I want to respond to multiple keys being pressed at the same time. I'm building a little game, where two fighters attack each other. When the user presses A on the keyboard, first fighter attacks, when the user presses J on the keyboard, the second fighter attacks. Then, I would like to have a combo attack, where when the user presses Q W E keys at the same time, It will deal extra damage, so how should I approach this problem ? What I have right now is this :

const getDamage = (attacker,defender) => {
  document.addEventListener("keydown", (e)=>{
    if(e.key ==  controls.playerOneAttack){
      console.log("DEFENDER " + (defender.health = defender.health - ( getHitPower(attacker) - getBlockPower(defender))))
      console.log(attacker.health);
    }
    else if(e.key == controls.playerTwoAttack){
      console.log("ATTACKER " + (attacker.health = attacker.health - (getHitPower(defender) - getBlockPower(attacker))))
      console.log(defender.health);
    }
   
  })
};


const getHitPower = (fighter) => {
  const criticalHitChance = Math.floor(Math.random() * 2 + 1);
  const power = fighter.attack * criticalHitChance;
  return power
};

const getBlockPower = (fighter) => {
  const dodgeChance = Math.floor(Math.random() * 2 + 1);
  const power = fighter.defense * dodgeChance;
  return power
};

I got controls coded in a different file and I'm just passing it in the if statement. Not asking to write a function for me, just asking how to build the if statement to respond to simultaneous key presses. Thank you in advance and sorry for the messy code, I'm a beginner :D

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