# Java: Processing can't run multiple key inputs simultaneously

**URL:** <https://discourse.processing.org/t/java-processing-cant-run-multiple-key-inputs-simultaneously/1575>\
**Category:** Coding Questions\
**Created:** [July 7, 2018, 2:09pm UTC](https://discourse.processing.org/t/java-processing-cant-run-multiple-key-inputs-simultaneously/1575 "2018-07-07T14:09:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brian/32/674_2.png) [@Brian](https://discourse.processing.org/u/Brian)\
**Post date:** [July 7, 2018, 2:09pm UTC](https://discourse.processing.org/t/java-processing-cant-run-multiple-key-inputs-simultaneously/1575/1 "2018-07-07T14:09:52Z")

</div>

I’m making a 2-player Ping pong game. I tried moving 1 player at a time, it worked fine but when I held down the move buttons of both at once, one moved flawlessly but the other moved just a bit then it got stuck.

```auto
final int playerW = 15;
final int playerH = 200;
Player p1 = new Player(0, 100, playerW, playerH);
Player p2 = new Player(600 - playerW, 100, playerW, playerH);

void setup() {
  size(600, 400);
}

void draw() {
  background(50);
  p1.display();
  p2.display();
  p1.move1();
  p2.move2();
}

class Player {
  int xPos, yPos, pW, pH;
  int pV = 10;
  Player(int x, int y, int w, int h) {
    this.xPos = x;
    this.yPos = y;
    this.pW = w;
    this.pH = h;
  }
  
  void display() {
    fill(240);
    rect(this.xPos, this.yPos, this.pW, this.pH);
  }

  void move1() { //<-----------------------player1's move function
    if (keyPressed == true) {
      if (key == 'w') {
        this.yPos -= pV;
      }
      if (key == 's') {
        this.yPos += pV;
      }
    }
  }
  
  void move2() { //<-----------------------player2's move function
    if (keyPressed == true) {
      if (keyCode == UP) {
        this.yPos -= pV;
      }
      if (keyCode == DOWN) {
        this.yPos += pV;
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![DogeMastr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dogemastr/32/689_2.png) [@DogeMastr](https://discourse.processing.org/u/DogeMastr)\
**Post date:** [July 7, 2018, 2:29pm UTC](https://discourse.processing.org/t/java-processing-cant-run-multiple-key-inputs-simultaneously/1575/2 "2018-07-07T14:29:47Z")

</div>

The way I fixed this (when I also made a pong game lol) I had an ArrayList that added a key when ever its held down and then removes it whenever you let go of it,  
along the lines of this:

```auto
void keyTyped() {
  if (!keysIn.contains(key)) {
    keysIn.add(key);
  }
}
void keyReleased() {
  if (keysIn.contains(key)) {
    keysIn.remove(new Character(key));
  }
}

```

KeysIn being the `ArrayList<Character>`

you can then check for a letter in that arraylist and continue with your program

---

<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:** [July 7, 2018, 4:42pm UTC](https://discourse.processing.org/t/java-processing-cant-run-multiple-key-inputs-simultaneously/1575/3 "2018-07-07T16:42:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [July 7, 2018, 4:42pm UTC](https://discourse.processing.org/t/java-processing-cant-run-multiple-key-inputs-simultaneously/1575/4 "2018-07-07T16:42:38Z")

</div>

The solution offered by @DogeMastr will work, but you could also do this more directly by using a separate variable for each key. Here is an example:

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

float circleX = 50;
float circleY = 50;

void draw() {
  background(200);  
  
  if (upPressed) {
    circleY--;
  }
  
  if (downPressed) {
    circleY++;
  }
  
  if (leftPressed) {
    circleX--;
  }
  
  if (rightPressed) {
    circleX++;
  }
  
  ellipse(circleX, 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;
  }
}

```

This code uses four `boolean` values to hold whether any of the array keys are pressed. In the `keyPressed()` function, the corresponding variable is set to `true`, and in the `keyReleased()` function, the corresponding variable is set to `false`. Then the `draw()` function uses those variable to move the circle depending on which arrow keys are currently pressed.

![circle moving in window](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/d726edb57de8851431b9216dafc6af79a25fb584.gif)

Shameless self-promotion: [here](http://happycoding.io/tutorials/processing/input#handling-multiple-keys) is a tutorial on this approach.

You could also do something like use an array to store the keys being pressed, but honestly I like to use specific variables because it make the code easier to understand.

But in the end you should take whichever approach makes the most sense to you.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [July 7, 2018, 4:43pm UTC](https://discourse.processing.org/t/java-processing-cant-run-multiple-key-inputs-simultaneously/1575/5 "2018-07-07T16:43:17Z")

</div>

@GoToLoop Please provide a description when you post links.
