# How do I allow two players to play at once using only one keyboard?

**URL:** <https://discourse.processing.org/t/how-do-i-allow-two-players-to-play-at-once-using-only-one-keyboard/3320>\
**Category:** Beginners\
**Created:** [September 6, 2018, 2:37pm UTC](https://discourse.processing.org/t/how-do-i-allow-two-players-to-play-at-once-using-only-one-keyboard/3320 "2018-09-06T14:37:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![domjanabi](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@domjanabi](https://discourse.processing.org/u/domjanabi)\
**Post date:** [September 6, 2018, 2:37pm UTC](https://discourse.processing.org/t/how-do-i-allow-two-players-to-play-at-once-using-only-one-keyboard/3320/1 "2018-09-06T14:37:08Z")

</div>

I tried recreating pong and it went pretty well … except the part where you actually play the game.

It doesn’t work with keyPressed() because I would need at least two instances of the key variable running at the same time, since i want the two players to be able to press and use multiple keys at the same time.

```auto
//yrect and yrect2 are the y positions of the two players
//rectspeed is the rate at which the players move along the y axis when a key is pressed

//controls for player 1
void control()
{
  keyPressed();
  
  if (keyPressed&&keyCode==UP)  
  {
    yrect2-=rectspeed;
  } 

  if (keyPressed&&keyCode==DOWN) 
  {
    yrect2+=rectspeed;
  }
  
}
//controls for player 2
void control2()
{
  keyPressed();
  if (keyPressed&&keyCode==SHIFT)  
  {
    yrect-=rectspeed;
  } 

  if (keyPressed&&keyCode==CONTROL) 
  {
    yrect+=rectspeed;
  }
  
}

```

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [September 6, 2018, 3:57pm UTC](https://discourse.processing.org/t/how-do-i-allow-two-players-to-play-at-once-using-only-one-keyboard/3320/2 "2018-09-06T15:57:21Z")

</div>

So I answered a question kind of similar to this, and well this is what I would personally do when it comes to handling multiple key presses. The only problem in your case, is that I do not know necessarily how many maximum keys will be pressed at one time, so you would have to work that out (and we could help out of course if need be).

The question is here:

> [@Key != the pressed key when control is held](https://discourse.processing.org/t/key-the-pressed-key-when-control-is-held/2779/3):
>
> So I borrowed a lot from this old post on an older Processing forum, credit to @GoToLoop : [Check this out:](https://forum.processing.org/two/discussion/16594/can-multiple-keys-be-pressed-on-the-keyboard) And I made some adjustments to fit what I think it is you are trying to do: boolean controlPressed, shiftPressed, vPressed, cPressed; //define all the booleans String keysPressed = ""; //have a string that will compile all the strings pressed String action = ""; //have a string that will display the end action void setup() { size(400,400); } void draw() { background(0); f…

And maybe you can take a look at that and see if it works? Now, as far as I know, technically you can’t really expect for two players to be able to play at the same time. I have made a two player pong game before, and had one player use the keyboard and the other uses the mouse. There might be a way to use two separate keyboards and switch between them but that’s just me being hypothetical 😃 As far as I know, it’s not ideal trying to get two players to use the same keyboard.

Hopefully that helps,

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![domjanabi](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@domjanabi](https://discourse.processing.org/u/domjanabi)\
**Post date:** [September 6, 2018, 4:52pm UTC](https://discourse.processing.org/t/how-do-i-allow-two-players-to-play-at-once-using-only-one-keyboard/3320/3 "2018-09-06T16:52:39Z")

</div>

i actually started off by controlling the rectangle with the a mouse but since i wanted to make a two player version of this, i thought it would be a good idea if both players had equal opportunity. Now that i think about it though, I could buy an arduino and set up two potentiometers,one for each player.  
Just like this guy did:

> **[Getting Started With Arduino - Potentiometer Pong](https://www.instructables.com/id/Getting-started-with-Arduino-Potentiometer-Pong/)**
>
> Getting Started With Arduino - Potentiometer Pong: In this tutorial we are going to build an arduino-based controller that we can use to play pong.We are going to use a dial potentiometer (a turning knob) to control the pong bat/paddle.

I don’t think I’ll be trying to do this on a keyboard anymore, but still, thanks for the help ^-^

---

<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:** [September 6, 2018, 4:58pm UTC](https://discourse.processing.org/t/how-do-i-allow-two-players-to-play-at-once-using-only-one-keyboard/3320/4 "2018-09-06T16:58:18Z")

</div>

Shameless self-promotion: check out the “handling multiple key presses” section of this tutorial:

> **[Getting User Input](https://happycoding.io/tutorials/processing/input)**
>
> Learn how to detect user input to make interactive programs in Processing.

Basically, you want to create your own variables that track the keys, then set them in the `keyPressed()` and `keyReleased()` functions.

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [September 6, 2018, 6:38pm UTC](https://discourse.processing.org/t/how-do-i-allow-two-players-to-play-at-once-using-only-one-keyboard/3320/5 "2018-09-06T18:38:11Z")

</div>

That’s great! And an awesome solution 😃
