# Detect multiple key presses at once?

**URL:** <https://discourse.processing.org/t/detect-multiple-key-presses-at-once/40193>\
**Category:** Coding Questions\
**Created:** [December 17, 2022, 3:21pm UTC](https://discourse.processing.org/t/detect-multiple-key-presses-at-once/40193 "2022-12-17T15:21:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![REK166](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@REK166](https://discourse.processing.org/u/REK166)\
**Post date:** [December 17, 2022, 3:21pm UTC](https://discourse.processing.org/t/detect-multiple-key-presses-at-once/40193/1 "2022-12-17T15:21:02Z")

</div>

Is there a way to access all buttons currently pressed?  
If not, why? And if there is, how?

---

<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:** [December 17, 2022, 3:39pm UTC](https://discourse.processing.org/t/detect-multiple-key-presses-at-once/40193/3 "2022-12-17T15:39:38Z")

</div>

> **[r/processing - How would I make a keyPressed function support multiple keys?](https://www.reddit.com/r/processing/comments/bshqa8/comment/eor5epo/)**
>
> 2 votes and 13 comments so far on Reddit

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [December 17, 2022, 6:24pm UTC](https://discourse.processing.org/t/detect-multiple-key-presses-at-once/40193/4 "2022-12-17T18:24:13Z")

</div>

Be aware that keyboards cannot read all key presses at the same time. Due either to the way they’re wired or the way the key press data is encoded, some combinations of keys will mask later keys. What’s worse is that it can be different between different keyboards.

You can try this out on your keyboard with this code. On my HP desktop keyboard, for instance, I can only see 3 of the arrow keys at once. Be sure to test this with the key combinations you want before thinking your code might have a bug somewhere else.

```auto
boolean[] keysDown;
boolean bHigh = false;

void setup() {
  size( 640, 640 );
  keysDown = new boolean[256];
}

void draw() {
  background(bHigh ? 128 : 0);
  noStroke();
  fill( 255 );
  for( int j=0; j<16; j++ )
    for( int i=0; i<16; i++ )
      if( keysDown[j*16+i] )
        text( str(j*16+i), i*40, j*40 );
}

void keyPressed() {
  if( keyCode < 256 ) 
    keysDown[keyCode] = true;
  else bHigh = true;
}

void keyReleased() {
  if( keyCode < 256 ) 
    keysDown[keyCode] = false;
  else bHigh = false;
}

```
