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

192
Views
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
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!