# Is three keys maximum?

**URL:** https://discourse.processing.org/t/is-three-keys-maximum/10090
**Category:** Coding Questions
**Created:** [April 8, 2019, 1:02pm UTC](https://discourse.processing.org/t/is-three-keys-maximum/10090 "2019-04-08T13:02:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lilleGauss](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@lilleGauss](https://discourse.processing.org/u/lilleGauss)
#### Post date: [April 8, 2019, 1:02pm UTC](https://discourse.processing.org/t/is-three-keys-maximum/10090/1 "2019-04-08T13:02:30Z")

</div>

Hello!

I am trying to get keyPressed/keyReleased to work for several keys simultaneously. Every single stroke works and two at the same time too. But nerver for all four of them? Is there somthing with internal handling of key events that limits it to max three key? Here is my example code:

```auto
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;

float circleX = 100;
float circleY = 100;

void setup(){
  size(200,200);
}

void draw() {
  background(200);  
  
  if (upPressed) {
    ellipse(circleX, circleY-30, 20, 20);
  }
  
  if (downPressed) {
    ellipse(circleX, circleY+30, 20, 20);
  }
  
  if (leftPressed) {
    ellipse(circleX-30, circleY, 20, 20);
  }
  
  if (rightPressed) {
    ellipse(circleX + 30, circleY, 20, 20);
  }
  
  
}

void keyPressed() {
  if (keyCode == UP) {
    upPressed = true;
  }
  else if (keyCode == DOWN) {
    downPressed = true;
  }
  else if (keyCode == LEFT) {
    leftPressed = true;
  }
  else if (keyCode == RIGHT) {
    rightPressed = true;
  }
}

void keyReleased() {
  if (keyCode == UP) {
    upPressed = false;
  }
  else if (keyCode == DOWN) {
    downPressed = false;
  }
  else if (keyCode == LEFT) {
    leftPressed = false;
  }
  else if (keyCode == RIGHT) {
    rightPressed = false;
  }
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 8, 2019, 1:10pm UTC](https://discourse.processing.org/t/is-three-keys-maximum/10090/2 "2019-04-08T13:10:23Z")

</div>

your code works here: ( win 7 / 64b // with logitech K100 keyboard // processing 3.5.3)

 ![SNAG-0064](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/05c4fd67ff850dd645f04d47e743261b2b958064.jpeg)

for test i pressed up down left right.  
for making the snapshot i needed [ctrl][shift][p]  
( all 7 key at the same time )  
so lucky i can show something

---

<div class="post-metadata">

### Author: ![lilleGauss](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@lilleGauss](https://discourse.processing.org/u/lilleGauss)
#### Post date: [April 8, 2019, 2:06pm UTC](https://discourse.processing.org/t/is-three-keys-maximum/10090/3 "2019-04-08T14:06:47Z")

</div>

Ok.  
So if it works for you the code seems to be OK and there is an issue with the keyboard driver or something.  
I will search in this direction.  
Thans for your help

Best Regards  
Ralf

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 10, 2019, 4:46am UTC](https://discourse.processing.org/t/is-three-keys-maximum/10090/4 "2019-04-10T04:46:53Z")

</div>

It sounds like you are using 2-key rollover hardware. The number of supported simultaneous presses is hardware specific. To learn more about it, read about n-key rollover (nkro): [https://en.wikipedia.org/wiki/Rollover\_(key)](https://en.wikipedia.org/wiki/Rollover_(key))

If you do want to go down the road of tracking each up/down state, I recommend something like a hashmap, rather than a long list of individual global variables. See for example:

- [https://forum.processing.org/two/discussion/comment/122623/#Comment\_122623](https://forum.processing.org/two/discussion/comment/122623/#Comment_122623)

---

<div class="post-metadata">

### Author: ![lilleGauss](https://avatars.discourse-cdn.com/v4/letter/l/8797f3/32.png) [@lilleGauss](https://discourse.processing.org/u/lilleGauss)
#### Post date: [April 11, 2019, 1:02pm UTC](https://discourse.processing.org/t/is-three-keys-maximum/10090/5 "2019-04-11T13:02:20Z")

</div>

Thanks for your reply!  
Always something new to learn!

I am aware of the solution with HashMaps but the newbies I am teaching have enough struggle with primitive variables. I keep those data structures like HashMap for later.

Best Regards  
Ralf

---

<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: [April 11, 2019, 1:33pm UTC](https://discourse.processing.org/t/is-three-keys-maximum/10090/6 "2019-04-11T13:33:31Z")

</div>

> [@lilleGauss](#):
>
> I keep those data structures like HashMap for later.

A non-HashMap sketch example:  
[Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G)
