# KeyPressed() for multiple keys pressed at the same time

**URL:** <https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892>\
**Category:** Processing\
**Created:** [March 22, 2020, 3:08am UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892 "2020-03-22T03:08:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)\
**Post date:** [March 22, 2020, 3:08am UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892/1 "2020-03-22T03:08:10Z")

</div>

Hi everyone,

I’m running into a confusing issue with the keyPressed() function when I have multiple keys pressed at the same time. It seems like after two or more keys are pressed the keyPressed() functionality no longer works correctly. This issue has been discussed before in this [thread](https://discourse.processing.org/t/keypressed-events-get-missed-when-another-key-is-held/12696) and there was no solution found. The outcome of the thread was that there is a bug in processing code which causes keyPressed() events to be missed.

Below is the code I’m using to show the keyPressed() errors.

```auto

boolean[] moveKeys = new boolean[4];

void setup() {
}

void draw() {
  println();
  println("w: "+moveKeys[0]);
  println("a: "+moveKeys[1]);
  println("s: "+moveKeys[2]);
  println("d: "+moveKeys[3]);
  println();
}

void setMovement(int k, boolean b) {
  switch (k) {
  case 'w':
    moveKeys[0] = b;
    break;
  case 'a':
    moveKeys[1] = b;
    break;
  case 's':
    moveKeys[2] = b;
    break;
  case 'd':
    moveKeys[3] = b;
    break;
  }
}

void keyPressed() {
  setMovement(key, true);
}

void keyReleased() {
  setMovement(key, false);
}

```

For example, when pressing down all four keys (w, a, s, d) it is showing that only three of the keys have been pressed.

```auto
w: true
a: true
s: false
d: true

```

I would really appreciate some brainstorming on this problem. I’m working on a dungeon crawler and am working on player movement now. I would like the player to be able to use multiple keys at the same time and the direction of movement be determined from that.

Thank you!  
Grayson

---

<div class="post-metadata">

**Author:** ![Tomfoolery](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@Tomfoolery](https://discourse.processing.org/u/Tomfoolery)\
**Post date:** [March 22, 2020, 5:43am UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892/2 "2020-03-22T05:43:36Z")

</div>

Well, I don’t know if this is helpful or not but I ran your code and it seems to be working just fine on my machine.  
I’m a bit of a novice so the only troubleshooting advice I can contribute is to ignore processing’s keyPressed() methods and set up Java’s key event listener. Maybe you’ll have better luck using lower level code. If anyone feels that wouldn’t work or is redundant in some way please feel free to educate me.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 22, 2020, 7:11am UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892/3 "2020-03-22T07:11:32Z")

</div>

> [@graysonh](#):
>
> For example, when pressing down all four keys (w, a, s, d) it is showing that only three of the keys have been pressed.

> [@Pressing multiple keys at once on different rows](https://discourse.processing.org/t/pressing-multiple-keys-at-once-on-different-rows/1023/2):
>
> [http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G)

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [March 22, 2020, 10:35am UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892/4 "2020-03-22T10:35:03Z")

</div>

I believe you have an issue that isn’t related to that other thread (your sketch isn’t running on P2D/P3D which is the only renderers that have that other thread’s issue), and isn’t related to Processing. Very many keyboards simply can’t detect when multiple keys that are nearby are pressed, as their circuitry doesn’t allow them to do so.

I’m experiencing the exact same problem with your sketch with my cheap keyboard, but I’m also experiencing it on any program and any OS on any computer I plug this keyboard into. The only solution is **to get a better keyboard!** 😃

(or, design sketches to overcome around this problem so it doesn’t matter in the first place)

---

<div class="post-metadata">

**Author:** ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)\
**Post date:** [March 22, 2020, 7:31pm UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892/5 "2020-03-22T19:31:43Z")

</div>

I’m seeing the same issue ocurring on the sketch you shared. It seems like the issue is with my keyboard and not with the keyPressed() event.

---

<div class="post-metadata">

**Author:** ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)\
**Post date:** [March 22, 2020, 7:33pm UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892/6 "2020-03-22T19:33:02Z")

</div>

I think you’re right about that. My keyboard is the issue here! Ill have to come up with a different solution to the problem so the keyboard being used won’t matter.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 22, 2020, 9:57pm UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892/7 "2020-03-22T21:57:51Z")

</div>

> [@graysonh](#):
>
> It seems like the issue is with my keyboard and not with the **keyPressed()** event.

Although it seems like my reply only had a link for a keyboard test sketch, if you click on its arrow down icon 🔽 it’s gonna reveal a link to a Wikipedia article as well: 🥴

> **[Key rollover](https://en.wikipedia.org/wiki/Rollover_(key))**
>
> Key rollover is the ability of a computer keyboard to correctly handle several simultaneous keystrokes. A keyboard with n-key rollover (NKRO) can correctly detect input from each key on the keyboard at the same time, regardless of how many other keys are also being pressed. Keyboards that lack full rollover will register an incorrect keystroke when certain combinations of keys are pressed simultaneously. Rollover has applications for stenotype, electronic music keyboards, gaming, and touch-typing...

---

<div class="post-metadata">

**Author:** ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)\
**Post date:** [March 24, 2020, 9:47pm UTC](https://discourse.processing.org/t/keypressed-for-multiple-keys-pressed-at-the-same-time/18892/8 "2020-03-24T21:47:35Z")

</div>

That a very interesting read! Now I know why I was having trouble with the WASD keys. There’s a key matrix involved that’s causing ghosting. Thanks for the help!
