KeyPressed() for multiple keys pressed at the same time

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 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.


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.

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

1 Like

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.

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! :smiley:

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

3 Likes

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.

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.

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

1 Like

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!