# Recognizing two keyboard inputs at the same time

**URL:** <https://discourse.processing.org/t/recognizing-two-keyboard-inputs-at-the-same-time/2389>\
**Category:** Processing\
**Created:** [August 5, 2018, 7:18pm UTC](https://discourse.processing.org/t/recognizing-two-keyboard-inputs-at-the-same-time/2389 "2018-08-05T19:18:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Stekken](https://avatars.discourse-cdn.com/v4/letter/r/b5e925/32.png) [@Ryan\_Stekken](https://discourse.processing.org/u/Ryan_Stekken)\
**Post date:** [August 5, 2018, 7:18pm UTC](https://discourse.processing.org/t/recognizing-two-keyboard-inputs-at-the-same-time/2389/1 "2018-08-05T19:18:10Z")

</div>

Hi and sorry for my bad english first.  
I’m creating a multiplayer-mode for my little pong game at the moment, but when both players try to move their “character”, only one of them reacts.  
my pretty simple code:

```auto
void draw ()
{
  background(0);
  
  strokeWeight(playerspeed);
  stroke(255);
  strokeCap(SQUARE);
  line(width/2,3*playerspeed,width/2,height-3*playerspeed);
  
  rect(xPosP1 , yPosP1 , playerWidth , playerHeight);
  rect(xPosP2 , yPosP2 , playerWidth , playerHeight);
  rect(xBall , yBall , ballWidth , ballHeight);
  player1();
  player2();
  
}

void player1 ()
{
  
  if (keyPressed)
  {
    if (key == 'w' || key == 'W')
    {
      yPosP1 -= playerspeed;
    } else if (key == 's' || key == 'S')
    {
      yPosP1 += playerspeed;
    }
  }

  if (yPosP1 < playerHeight/2)
  {
    yPosP1 = playerHeight/2;
  } else if (yPosP1 > height - (playerHeight/2))
  {
    yPosP1 = height - (playerHeight/2);
  }

}

void player2 ()
{
   if (keyPressed)
  {
    if (key == 'k' || key == 'K')
    {
      yPosP2 -= playerspeed;
    } else if (key == 'm' || key == 'M')
    {
      yPosP2 += playerspeed;
    }
  }

  if (yPosP2 < playerHeight/2)
  {
    yPosP2 = playerHeight/2;
  } else if (yPosP2 > height - (playerHeight/2))
  {
    yPosP2 = height - (playerHeight/2);
  }
  
}

```

How can I make Processing react to both inputs at the same time?  
By the way, I’m an absolute beginner.  
Can anyone help me?  
Thanks in advance. 😃

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [August 5, 2018, 7:33pm UTC](https://discourse.processing.org/t/recognizing-two-keyboard-inputs-at-the-same-time/2389/2 "2018-08-05T19:33:29Z")

</div>

The problem is that `key` only tracks the last key that was pressed. So with two players, they can’t both be pressing the last key!

You will need to restructure your code so that you are keeping track of which keys are held down. You can do this by making use of the `keypressed()` and `keyReleased()` functions:

```auto
char[] tracked_keys = ['w','s','k','m']
boolean[] key_down = { false, false, false, false };

void keyPressed(){
  keyTrack(true);
}

void keyReleased(){
  keyTrack(false);
}

void keyTrack(boolean b);
  for( int i = 0; i < tracked_keys.length; i++){
    if( key == tracked_keys[i] ){
      key_down[i] = b;
    }
  }
}

// if( key_down[0] ){ // 'w' is pressed

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Stekken](https://avatars.discourse-cdn.com/v4/letter/r/b5e925/32.png) [@Ryan\_Stekken](https://discourse.processing.org/u/Ryan_Stekken)\
**Post date:** [August 8, 2018, 11:06pm UTC](https://discourse.processing.org/t/recognizing-two-keyboard-inputs-at-the-same-time/2389/3 "2018-08-08T23:06:50Z")

</div>

Thank you! It works! 🙂
