Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda