By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to detect and map many keys being pressed at once
JavaScript

Javascript – How to detect and map many keys being pressed at once

Admin
Last updated: 2023/12/14 at 4:01 AM
Admin
Share
2 Min Read
How to detect and map many keys being pressed at once

Problem:

I am using the keydown and keyup event to detect key presses. But when I press 2 or 3 or more keys and hold them down, most of the time it won’t recognize any more keys pressed after, until I stop holding down the keys. I am looking to detect any number of keys pressed at once.

Contents
Problem:Solution:

It seems all of the other posts I’ve looked at are only about figuring out how to map multiple keys pressed at once, but not about this particular issue I’ve run into.

This is how I map key presses in my code:

// a map of press-able keys
const key = {
  w: false,
  a: false,
  s: false,
  d: false
};

document.addEventListener('keydown', function(e) {

  if (key[e.key.toLowerCase()] != undefined) {
    key[e.key.toLowerCase()] = true;
  }

});

document.addEventListener('keyup', function(e) {

  if (key[e.key.toLowerCase()] != undefined) {
    key[e.key.toLowerCase()] = false;
  }

});

For example, if I held down W, A, S, and D simultaneously, most of the time, it would only recognize the first 2 or 3 I pressed, and ignore the last 1 or 2 (key.a and key.w might be true, but key.s and key.d might be false, even though they’re being pressed down).

Is there something wrong with this implementation? Or is there a limit to the keydown event I haven’t been able to find in searching? What is a potential solution for being able to detect many keys at once?

Solution:

Keyboard ghosting: is a phenomenon in which a computer keyboard fails to accurately register simultaneous key presses, often affecting gaming and typing experiences. Anti-ghosting technology is employed to mitigate this issue by ensuring the correct recognition of multiple key inputs.

Microsoft provides an interactive demo, you can use it to test your keyboard.
https://www.microsoft.com/applied-sciences/projects/anti-ghosting-demo

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy1
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to get prettier errors in terminal when starting react app? Javascript – How to get prettier errors in terminal when starting react app?
Next Article TypeError: uniqueCategories.map is not a function Javascript – TypeError: uniqueCategories.map is not a function
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?