# Keyboard controls for multiplayer games

**URL:** <https://discourse.processing.org/t/keyboard-controls-for-multiplayer-games/14580>\
**Category:** Coding Questions\
**Created:** [October 10, 2019, 4:55pm UTC](https://discourse.processing.org/t/keyboard-controls-for-multiplayer-games/14580 "2019-10-10T16:55:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KowtowToMao](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@KowtowToMao](https://discourse.processing.org/u/KowtowToMao)\
**Post date:** [October 10, 2019, 4:55pm UTC](https://discourse.processing.org/t/keyboard-controls-for-multiplayer-games/14580/1 "2019-10-10T16:55:58Z")

</div>

I’m trying to make a variant of pong, as well as a tanks game, but I can’t find a good way to make multiplayer work. Advice? Current method in the pong variant is shown below, but it has trouble taking multiple inputs at the same time.

Code from main file:

```auto
void keyReleased(){
	if(keyCode==UP){
		g.p1.isMovingUp=false;
	}
	if(keyCode==DOWN){
		g.p1.isMovingDown=false;
	}
	if(key=='w'||key=='W'){
		g.p2.isMovingUp=false;
	}
	if(key=='s'||key=='S'){
		g.p2.isMovingDown=false;
	}
}

```

Code from paddle class:

```auto
public void moveUp(){
	if(isMovingUp&&y>0){
		y-=speed;
	}
}
public void moveDown(float h){
	if(isMovingDown&&y+pHeight<h){
		y+=speed;
	}
}

```

Code in game class:

```auto
if(keyPressed){
	if(keyCode==UP&&p1.getY()>0){
		p1.isMovingUp=true;
		p1.isMovingDown=false;
	}
	if(keyCode==DOWN&&p1.getY()+p1.getHeight()<gHeight){
		p1.isMovingDown=true;
		p1.isMovingUp=false;
	}
	if((key=='w'||key=='W')&&p2.getY()>0){
		p2.isMovingUp=true;
		p2.isMovingDown=false;
	}
	if((key=='s'||key=='S')&&p2.getY()+p2.getHeight()<gHeight){
		p2.isMovingDown=true;
		p2.isMovingUp=false;
	}
}

```

Full program: [https://www.openprocessing.org/sketch/747012](https://www.openprocessing.org/sketch/747012)

---

<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:** [October 10, 2019, 11:24pm UTC](https://discourse.processing.org/t/keyboard-controls-for-multiplayer-games/14580/2 "2019-10-10T23:24:02Z")

</div>

i made a runner what easy  
now also works in 2 player(hand) / one keyboard / mode,  
also the diagonal running works nice ( so up to 4 keys work parallel )

```auto
// 2 player / hand mode for 2 ( diagonal ) runner 

int[] p1keysb={0,0,0,0}; // pressed key memory, NOW INT SPEED
char[] p1keyss={'w','s','a','d'}; // key list to be detected parallel UP DOWN LEFT RIGHT
int[] p2keysb={0,0,0,0}; // pressed key memory, NOW INT SPEED
char[] p2keyss={38,40,37,39}; // key CODE to be detected parallel UP DOWN LEFT RIGHT
int x1=0, y1, x2=0, y2, speed = 3, size = 10;

void setup() {
  size(400, 400);
  x2 = width;
  noStroke();
  print_keyinfo();
}

void draw() {
  background(255);
  move();
}

void print_keyinfo() {
  print("Player 1 use keys:");
  for ( int i=0;i<4;i++) print("["+p1keyss[i]+"] ");
  print("\nPlayer 2 use keys:");
  for ( int i=0;i<4;i++) print("["+p2keyss[i]+"] ");
  println("\nfor UP DOWN LEFT RIGHT");
}

void move() { // and draw
    x1 = constrain(x1 += p1keysb[3]-p1keysb[2] , 0, width-size-1);
    y1 = constrain(y1 += p1keysb[1]-p1keysb[0] , 0, height-size-1);
    fill(200,0,0);
    square(x1, y1, size);
    x2 = constrain(x2 += p2keysb[3]-p2keysb[2] , 0, width-size-1);
    y2 = constrain(y2 += p2keysb[1]-p2keysb[0] , 0, height-size-1);
    fill(0,0,200);
    square(x2, y2, size);
}

void keyPressed() {
  //println("key "+key+" keyCode "+keyCode);
  for ( int i=0;i<4;i++) if ( key ==p1keyss[i] ) p1keysb[i]=speed;
  for ( int i=0;i<4;i++) if ( keyCode==p2keyss[i] ) p2keysb[i]=speed;
}

void keyReleased() {
  for ( int i=0;i<4;i++) if ( key ==p1keyss[i] ) p1keysb[i]=0;
  for ( int i=0;i<4;i++) if ( keyCode==p2keyss[i] ) p2keysb[i]=0;
}

```

hope that idea helps.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 11, 2019, 12:27am UTC](https://discourse.processing.org/t/keyboard-controls-for-multiplayer-games/14580/3 "2019-10-11T00:27:19Z")

</div>

This topic may be of interest:

> [@Solution for repeating keyPressed while holding key](https://discourse.processing.org/t/solution-for-repeating-keypressed-while-holding-key/3989):
>
> I came up with a solution for a problem I’ve encountered regarding inputs. The problem: When you hold down a key, at least on my computer it registers as if you were repeatedly pressing that key very fast. In some cases you only want to get one single registered keypress, even if the key is held down. Just to clarify my point with a stupid example: say you were creating a game, and the goal of the game is to press a single key repeatedly, as fast as possible: int totalPresses = 0; void setup…

I adapted for multiple key presses.

🙂
