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

632
Views
How To Detect Two Characters (Keys) Pressed At The Same TIme

An important distinction is that it is easier to know if cntrl and z are pressed at the same time, but not as straight forward to detect if z and x are pressed at the same time. What is the best way to go about detecting when multiple characters are pressed at the same time?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This example uses event listeners to keep track of an object which contains all keys currently being pressed.

We can use this object to check if certain keys are pressed. This means that we can detect as many simultaneous key presses as a user's keyboard will allow.

Importantly it will let you detect when multiple characters are pressed at the same time.

const keysPressed = {};

document.addEventListener('keydown', (event) => {
  if (!event.repeat)
    keysPressed[event.key] = true;
  
  //You can also include event.repeat as a condition in your cases if you wish an event to only occur once each trigger.
  //You can also include Object.keys(keysPressed).length === 2 (for example) if you only wish the case to be valid if just two keys are pressed.
  //Finally, instead of event.key === 'x' you can write keysPressed['x']. This would replace both non-default cases in the example. The result is that the event will continue firing if another key is pressed.
  switch (true) { //You use true here because we are using logic to determine each case
    case event.key === 'x' && keysPressed['z']:
    case event.key === 'z' && keysPressed['x']:
      console.log('Z and X pressed!');
      break;
    default:
      break;
  }
});

document.addEventListener('keyup', (event) => {
  if (!event.repeat)
    delete keysPressed[event.key];
});

about 4 years ago · Juan Pablo Isaza Report

0

I'm simply using an array as storage for all keys that are pressed. This array is cleaned to an empty state when keys are released.

In keydown event listener, you can hook the function with logic that you will want to implement. Read keys array to check all currently pressed keys.

const keys = []
document.addEventListener('keydown', e => {
  !event.repeat && keys.push(e.key)
  doSomethingWithKeys(keys)
})
document.addEventListener('keyup', e => {
  keys.splice(0, keys.length)
})

function doSomethingWithKeys(keys) {
  console.log('keys pressed:', keys.join('+'))
}

enter image description here

about 4 years ago · Juan Pablo Isaza Report
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!