# How to register 2 keys at a time

**URL:** <https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635>\
**Category:** Coding Questions\
**Created:** [October 19, 2018, 11:44am UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635 "2018-10-19T11:44:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [October 19, 2018, 11:44am UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635/1 "2018-10-19T11:44:57Z")

</div>

Hi,  
so I’m trying to make a small game in which you control a litlle space ship and dodge some stuff. Something like that. The player should be able to move in 8 directions, including the 4 diagonal ones. I guess you can imagine what I mean by that. 🙂 There’s only one problem. Processing only seems to register one input at a time the way I wrote my code. For example, it’s possible to move up or left by pressing the arrwow keys, but not both at the same time, How can I do that?

Heres my code:

```auto
float[] plane = {350, 700, 50, 70}; //x,y,width,height

void setup() {
  size(700, 800);
  rectMode(CENTER);
}

void draw() {
  background(0);
  movePlane();
  plane();
}

void plane() {
  rect(plane[0], plane[1], plane[2], plane[3]);
}

void movePlane() {
  if (keyPressed && keyCode == LEFT) {
    plane[0] = plane[0] - 5;
  }
  if (keyPressed && keyCode == UP) {
    plane[1] = plane[1] - 5;
  }
  if (keyPressed && keyCode == RIGHT) {
    plane[0] = plane[0] + 5;
  }
  if (keyPressed && keyCode == DOWN) {
    plane[1] = plane[1] + 5;
  }
}

```

---

<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:** [October 19, 2018, 11:53am UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635/2 "2018-10-19T11:53:11Z")

</div>

[Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp](http://Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp)

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [October 20, 2018, 7:04am UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635/3 "2018-10-20T07:04:23Z")

</div>

I don’t know if you just want a hint at to how to do it yourself, or just a working code, so i’ll blur the code below the Spoiler sign and just give you some hints first 🙂

Processing takes input only with keyCode as far as i know, so theres not much you can do if you have multiple inputs at once, other than getting them one after the other. And thats impossible if you don’t have superhuman reflexes, so you have to save the input somehow, at least for the duration of that frame. Example, pressing left and right should not give you any result, but it will, because it will tell processing keyCode = left and then keyCode = right. Even if you are still holding left, once you press right keyCode is right. So you could navigate right by pressing left. What would be best in you case would be to save the input you can get. I.e: Up, Down, Left, Right. and then, every move just execute the ones that you got and that didn’t get turned down. Best would be to use Booleans. This way you can also move diagonally. Btw, theres also a keyReleased option, which you need to do this. Now to the Code:  
Spoiler : (just click on it to show it)

```auto
float[] plane = {350, 700, 50, 70}; //x,y,width,height
boolean up, down, left, right;

void setup() {
  size(700, 800);
  rectMode(CENTER);
}

void draw() {
  background(0);
  movePlane();
  plane();
}

void plane() {
  rect(plane[0], plane[1], plane[2], plane[3]);
}

void movePlane() {
  if (left) {
    plane[0] -= 5;
  }
  if (up) {
    plane[1] -= 5;
  }
  if (right) {
    plane[0] += 5;
  }
  if (down) {
    plane[1] += 5;
  }
}

void keyPressed() {
  // this basically means if (keyCode == UP) { up = true; } else { up = up; }
  //you can add the last part (up; down; and so) into false, then it will only get one input at once again.
  up = keyCode == UP ? true : up;
  down = keyCode == DOWN ? true : down;
  left = keyCode == LEFT ? true : left;
  right = keyCode == RIGHT ? true : right;
  
}

void keyReleased() {
  up = keyCode == UP ? false : up;
  down = keyCode == DOWN ? false : down;
  left = keyCode == LEFT ? false : left;
  right = keyCode == RIGHT ? false : right;
}

```

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [October 20, 2018, 8:56am UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635/4 "2018-10-20T08:56:20Z")

</div>

I found a good way of doing it a while ago:

Create an array `int[] keys = new int[1000];` Then in have

```auto
void keyPressed() {
  keys[keyCode] = true; // If you press the key it will be true in it's index in keys
}

void keyReleased() {
  keys[keyCode] = false; // When you let go it will return to false again
}

```

And if you want to know when keys are being pressed

```auto
if(keys[<Your keyCode>]) { // Check if the key is activated
  ... Code ...
} 

```

This will also work with all the keys, as the array stores all of the `keyCode`'s.

---

<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:** [October 20, 2018, 9:38am UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635/5 "2018-10-20T09:38:45Z")

</div>

[Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9cfU11egvzT6G)

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [October 20, 2018, 11:23am UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635/6 "2018-10-20T11:23:39Z")

</div>

If i got what you mean, then i think that should be a boolean array, not int. But if you only have 4 Inputs it is kinda Overkill to have an Array with 1000 Elements in it.

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [October 20, 2018, 12:28pm UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635/7 "2018-10-20T12:28:01Z")

</div>

Thanks! I’ll do it that way 😃

---

<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:** [October 23, 2018, 8:33pm UTC](https://discourse.processing.org/t/how-to-register-2-keys-at-a-time/4635/8 "2018-10-23T20:33:57Z")

</div>

Following up on @Sarah’s answer – I have shared a HashMap solution to this problem and ones like this a few times in the past. The HashMap approach is a bit nicer than int[1000] in that it can store any key you press, but ONLY stores the keys you press. Then instead of checking for keys, you do this:

```auto
void keyPressed(){
  flags.put(key, Boolean.TRUE);
}
void keyReleased(){
  flags.put(key, Boolean.FALSE);
}

```

Now you can check your map with get() and if it is true, the key is down.

Here is the code and explanation:

> [@Keyboard buttons](https://discourse.processing.org/t/keyboard-buttons/717/3):
>
> If you are doing more than a few keys, another general option is to use implement @TfGuy44’s boolean approach using a [HashMap](https://processing.org/reference/HashMap.html). import java.util.Map; HashMap\<Character,Boolean\> flags = new HashMap\<Character,Boolean\>(); Any key that gets pressed, put it in the map with a true… any key that gets released, take it out with a false. void keyPressed(){ flags.put(key, Boolean.TRUE); } void keyReleased(){ flags.put(key, Boolean.FALSE); } This can be used for simultaneous keypresses – or for a la…
