Cannot move both "players" at once. Don't understand why it cannot pick up multiple (keyPressed) at once

float circleX;
float circleY;
float allColor;
float bgColor;
float player1Y;
float player2Y;
float ballSpeedX;
float ballSpeedY;
float player1Score;
float player2Score;
float trueBallSpeed;
int player1ScoreView;
int player2ScoreView;

 
void setup() {
  circleX = width/15+width/35;
  circleY = height/2;
  allColor = 255;
  bgColor = 0;
  player1Y = height/2;
  player2Y = height/2;
  ballSpeedX = width/150; // ball speeeeeed
  ballSpeedY = 1;
  player1Score = 0;
  player2Score = 0;
  trueBallSpeed = width/1300;
  textSize(width/15);
  fullScreen();
  background(bgColor);
}
void draw() {
  fill(allColor);
  noStroke();
  background(bgColor);
  player1ScoreView = round(player1Score);
  player2ScoreView = round(player2Score);
  text(player1ScoreView, width/2-width/10, height/10);
  text(player2ScoreView, width/2+width/10, height/10);
  circle(circleX, circleY, width/35);
  stroke(allColor);
  strokeWeight(width/60);
  circleX = circleX+ballSpeedX;
  circleY = circleY+ballSpeedY;
  stroke(60);
  strokeWeight(width/700);
  line(width/2, 0, width/2, height);
  strokeWeight(20);
  stroke(allColor);
  line(width/15, player1Y-height/17, width/15, player1Y+height/17);
  line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
  if (circleX >= width*14/15-width/42 && circleY >= player2Y-height/13 && circleY <= player2Y+height/13 && circleX <= width*14/15+width/42) {
      trueBallSpeed = trueBallSpeed + width/1300;
      ballSpeedX = -width/150-trueBallSpeed;
      ballSpeedY = random(-width/130, width/130);
    }
    if (circleX <= width/15+width/35 && circleY >= player1Y-height/13 && circleY <= player1Y+height/13 && circleX >= width/15-width/42) {
      trueBallSpeed = trueBallSpeed + width/1300;
      ballSpeedX = width/150 + trueBallSpeed; // ball speeeeeeed
      ballSpeedY = random(-width/130, width/130);
    }
    if (circleY >= height-width/42) {
      ballSpeedY = -ballSpeedY;
    }
    if (circleY < 0+width/42) {
      ballSpeedY = -ballSpeedY;
    }
    if (player1Y >= height-height/12) {
      player1Y = height-height/12;
    }
    if (player1Y <= 0+height/12) {
      player1Y = 0+height/12;
    }
    if (player2Y >= height-height/12) {
      player2Y = height-height/12;
    }
    if (player2Y <= 0+height/12) {
      player2Y = 0+height/12;
    }
  if (keyPressed) {
    if (key == 'w' || key == 'W') {
      player1Y = player1Y-width/200;
      background(bgColor);
      line(width/15, player1Y-height/17, width/15, player1Y+height/17);
      line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
      text(player1ScoreView, width/2-width/10, height/10);
      text(player2ScoreView, width/2+width/10, height/10);
      noStroke();
      circle(circleX, circleY, width/35);
      stroke(60);
      strokeWeight(width/700);
      line(width/2, 0, width/2, height);
      strokeWeight(20);
      stroke(allColor);
    }
    if (key == 's' || key == 'S') {
      player1Y = player1Y+width/200;
      background(bgColor);
      line(width/15, player1Y-height/17, width/15, player1Y+height/17);
      line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
      text(player1ScoreView, width/2-width/10, height/10);
      text(player2ScoreView, width/2+width/10, height/10);
      noStroke();
      circle(circleX, circleY, width/35);
      stroke(60);
      strokeWeight(width/700);
      line(width/2, 0, width/2, height);
      strokeWeight(20);
      stroke(allColor);
    }
  }
  if (keyPressed) {
    if (keyCode == UP) {
      player2Y = player2Y-width/200;
      background(bgColor);
      line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
      line(width/15, player1Y-height/17, width/15, player1Y+height/17);
      text(player1ScoreView, width/2-width/10, height/10);
      text(player2ScoreView, width/2+width/10, height/10);
      noStroke();
      circle(circleX, circleY, width/35);
      stroke(60);
      strokeWeight(width/700);
      line(width/2, 0, width/2, height);
      strokeWeight(20);
      stroke(allColor);
    }
    if (keyCode == DOWN) {
      player2Y = player2Y+width/200;
      background(bgColor);
      line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
      line(width/15, player1Y-height/17, width/15, player1Y+height/17);
      text(player1ScoreView, width/2-width/10, height/10);
      text(player2ScoreView, width/2+width/10, height/10);
      noStroke();
      circle(circleX, circleY, width/35);
      stroke(60);
      strokeWeight(width/700);
      line(width/2, 0, width/2, height);
      strokeWeight(20);
      stroke(allColor);
    }
  }
  if(circleX >= width*3) {
    circleX = width/15+width/35;
    circleY = height/2; 
    player1Y = height/2;
    player2Y = height/2;
  }
  if(circleX <= -width*2) {
    circleX = width/15+width/35;
    circleY = height/2; 
    player1Y = height/2;
    player2Y = height/2;
  }
  if(circleX >= width) {
    background(206, 21, 21);
    line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
    line(width/15, player1Y-height/17, width/15, player1Y+height/17);
    text(player1ScoreView, width/2-width/10, height/10);
    text(player2ScoreView, width/2+width/10, height/10);
    noStroke();
    circle(circleX, circleY, width/35);
    stroke(60);
    strokeWeight(width/700);
    line(width/2, 0, width/2, height);
    strokeWeight(20);
    stroke(allColor);
  }
  if(circleX >= width*2+width/3) {
    background(0, 255, 0);
    line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
    line(width/15, player1Y-height/17, width/15, player1Y+height/17);
    text(player1ScoreView, width/2-width/10, height/10);
    text(player2ScoreView, width/2+width/10, height/10);
    noStroke();
    circle(circleX, circleY, width/35);
    stroke(60);
    strokeWeight(width/700);
    line(width/2, 0, width/2, height);
    strokeWeight(20);
    stroke(allColor);
    trueBallSpeed = width/1300;
  }
  if(circleX <= 0) {
    background(206, 21, 21);
    line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
    line(width/15, player1Y-height/17, width/15, player1Y+height/17);
    text(player1ScoreView, width/2-width/10, height/10);
    text(player2ScoreView, width/2+width/10, height/10);
    noStroke();
    circle(circleX, circleY, width/35);
    stroke(60);
    strokeWeight(width/700);
    line(width/2, 0, width/2, height);
    strokeWeight(20);
    stroke(allColor);
  }
  if(circleX <= -(width+width/3)) {
    background(0, 255, 0);
    line(width*14/15, player2Y-height/17, width*14/15, player2Y+height/17);
    line(width/15, player1Y-height/17, width/15, player1Y+height/17);
    text(player1ScoreView, width/2-width/10, height/10);
    text(player2ScoreView, width/2+width/10, height/10);
    noStroke();
    circle(circleX, circleY, width/35);
    stroke(60);
    strokeWeight(width/700);
    line(width/2, 0, width/2, height);
    strokeWeight(20);
    stroke(allColor);
    trueBallSpeed = width/1300;
  }
  if (circleX >= width && circleX <= width+width/162) {
    player1Score = player1Score + 1;
  }
  if (circleX <= 0 && circleX >= -(width/162)) {
    player2Score = player2Score + 1;
  }
  if (player1Score >= 10) {
    textAlign(CENTER);
    fill(0);
    background(255, 215, 0);
    textSize(width/5);
    text("Player 1", width/2, height/3);
    text("wins!", width/2, height*4/5);
  }
  if (player2Score >= 10) {
    textAlign(CENTER);
    fill(0);
    background(255, 215, 0);
    textSize(100);
    text("Player 2", width/2, height/3);
    text("wins!", width/2, height*4/5);
  }
}

https://www.Reddit.com/r/processing/comments/bshqa8/how_would_i_make_a_keypressed_function_support/eor5epo/

1 Like

Another example which works most of the time follows. I’m not sure who wrote the original demo.

int input[] = new int[2];
int keySelected;
boolean keyDwn = false;

final int _keyF = 70;
final int _keyJ = 74;

void setup() {
  size(400, 200);
  surface.setTitle("Press F and J keys (lower case is ok)");
  input[0] = _keyF;
  input[1] = _keyJ;
  rect(75, 50, 100, 100);
  rect(225, 50, 100, 100);
}

void draw() {
  switch(keySelected) {
  case _keyF:
    if (keyDwn) {
      println("key F dwn.");
      fill(0,255,0);
    } else {
      println("key F up.");
      fill(255,255,0);
    }
    rect(75, 50, 100, 100);
    break;
  case _keyJ:
    if (keyDwn) {
      println("key J down.");
      fill(255, 0, 0);
    } else {
      println("key J up.");
      fill(0, 0, 255);
    }
    rect(225, 50, 100, 100);
    break;
  }
}

void keyPressed() {
  for (int x = 0; x < input.length; x++) {
    if (input[x] == _keyF || input[x] == _keyJ) {
      keySelected = keyCode;
      keyDwn = true;
    }
  }
}

void keyReleased() {
  for (int x = 0; x < input.length; x++) {
    if (input[x] == _keyF || input[x] == _keyJ) {
      keySelected = keyCode;
      keyDwn = false;
    }
  }
}

keyPressed can only keep track of a single key at a time which is tracked in either key or keyCode, so when a second key is pressed, it forgets the first.

The code below keeps a boolean array with a value for each key that is true when a given key is pressed and false otherwise. You have to keep in mind, though, that most keyboards will not send out signals for all key press combinations and different keyboards will behave differently. On my HP keyboard, for instance, I can only read two out of the three keys of HJK at the same time. Whichever one I press third simply doesn’t send a signal. For the program below, try pressing different combinations of keys to see which works for you.

boolean[] keysDown;
boolean bHigh = false;

void setup() {
  size( 640, 640 );
  keysDown = new boolean[ 256 ];
}

void draw() {
  background(bHigh ? 128 : 0);
  noStroke();
  fill( 255 );
  for( int j=0; j<16; j++ )
    for( int i=0; i<16; i++ )
      if( keysDown[ j*16+i ] )
        text( str(j*16+i), i*40, 16+j*40 );
}

void keyPressed() {
  if( keyCode < 256 ) 
    keysDown[ keyCode ] = true;
  else bHigh = true;
}

void keyReleased() {
  if( keyCode < 256 ) 
    keysDown[ keyCode ] = false;
  else bHigh = false;
}
2 Likes

May I please get an explanation of this in simple terms? I have no idea what thus means.

this looks really good, and i can almost understand it but not quite. Could you please explain how it works?

What part do you not understand? Explanations by @GoToLoop and @scudly should be helpful. According to their entries (and as you have found out) the runtime has trouble keeping track of multiple key presses. However, by using an array coupled with keyPressed() and keyReleased() it can keep track of which key is pressed and whether it is down or up. There are two different color rectangles, one rectangle for each key, which could simulate two ‘players’. When a specified key is pressed, the assigned rectangle changes color. By looking at the color changes you should be able to see that it is possible to change the color of both rectangles simultaneously by pressing the assigned keys at the same time. Occasionally it fails (for reasons unknown to me), but it gets it right a high percentage of the time which should allow the code to be used for a simple game used for entertainment.

Below is a simplified version using a single boolean array which may be easier to understand.
Simplified Version:

boolean[] keyDwn = new boolean[2];

final int _keyF = 70;
final int _keyJ = 74;

void setup() {
  size(400, 200);
  surface.setTitle("Press F and J keys (lower case is ok)");
  keyDwn[0] = false;
  keyDwn[1] = false;
}

void draw() {  
  if(keyDwn[0] == true){
    fill(0,255,0);
  } else {
    fill(255,255,0);
  }
  rect(75, 50, 100, 100); // Player A
  
  if(keyDwn[1] == true){
    fill(255,0,0);
  } else {
    fill(0,0,255);
  }
  rect(225, 50, 100, 100); // Player B
}

void keyPressed() {
    if (keyCode == _keyF) {
      keyDwn[0] = true;
      println("keyF pressed = ", keyCode);
    }
    if(keyCode == _keyJ){
      keyDwn[1] = true;
       println("keyJ pressed = ", keyCode);
    }
}

void keyReleased() {    
     if (keyCode == _keyF) {
      keyDwn[0] = false;
    }
    if(keyCode == _keyJ){
      keyDwn[1] = false;
    }
}
2 Likes

Oh my gosh thank you so much! I got it working with this, you’re a lifesaver! Thank you svan!
I’ve been working on this one thing for days. Thank you.