When do you use keyPressed as function?

There seems to be multiple ways to use keyPressed.

if (keyPressed == true) { …

or
void keyPressed() { …

When do you use it as function and when not? When and how do you combine both?

Hi,

Looking at the Processing documentation, they say (about keyPressed() :

Note that there is a similarly named boolean variable called keyPressed.

So basically they act the same way but one is a system variable (keyPressed) accessible from everywhere and the other one is a function called when a key is pressed.

It looks that if you check is keyPressed is true in the draw() function, it can trigger multiple times whereas the keyPressed() function is only executing once. It depends on the usage and the problem you want to solve.

I usually prefer using the keyPressed() function as it’s more precise and clear in my code.

Note : Saying :

if (keyPressed == true) { …

can be simplified because keyPressed is already a boolean value so if it’s true then the condition is true then if it’s false the condition is false, so you can directly write :

if (keyPressed) { ...
2 Likes

And how do you use it in a class?
I’ve now:

class Paddle {
  float x, y, paddleW, paddleH;

  Paddle(float tempX, float tempY, float tempPaddleW, float tempPaddleH) {
    x = tempX;
    y = tempY;
    paddleW = tempPaddleW;
    paddleH = tempPaddleH;
  }


  void move() {
    if (keyPressed) {
      if (key == 'a') {
        x -= 2;
      }
      if (key == 'd') {
        x += 2;
      }
      if (x + (paddleW/2) <= 0) {
        x = -(paddleW/2);
      }
      if (x + (paddleW/2) >= width) {
        x = (width -  (paddleW/2));
      }
    }
  }

  void display() {
    fill(255);
    rect(x, y, paddleW, paddleH);
  }
}

Just call the move() method inside the keyPressed() function :

Paddle paddle;

void setup(){
  paddle = new Paddle(...);
}

void keyPressed(){
  paddle.move();
}

The nice thing about this is that you are only going to call the move() method when the user press a key rather than calling it every frame and performing the if statement to check whether a key was pressed.

Thx. I did try it quickly, but it seems there is some delay now between when I press button ‘a’ and the paddle going to the left side.

Ball ball0;
Ball ball1;

Paddle paddle0;

void setup() {
  size(200, 200);

  ball0 = new Ball(color(100, 0, 100), 10, 10, 20, random(1, 2), random(1, 1));
  ball1 = new Ball(color(random(255), random(255), random(255)), 10, 50, 10, random(1, 2), random(1, 2));

  paddle0 = new Paddle(width/2, height-10, 50, 10);
}


void draw() {
  background(0);
  ball1.move();
  ball1.display();


  ball0.move();
  ball0.display();


  paddle0.display();
  //paddle0.move();
}




class Ball {
  float x, y, diameter, xspeed, yspeed;
  color ballColor;

  Ball(color tempBallcolor, float tempX, float tempY, float tempDiameter, float tempXspeed, float tempYspeed) {
    ballColor = tempBallcolor;
    x = tempX;
    y = tempY;
    diameter = tempDiameter;
    xspeed = tempXspeed;
    yspeed = tempYspeed;
  }

  void move() {
    y += yspeed;
    x += xspeed;

    //xboundary
    if (x > width) {
      xspeed = random(-1, -2);
    }
    if (x <= 0) {
      xspeed = random(1, 2);
    }

    //yboundary
    if (y >= height) {
      yspeed = random(-1, -2);
    }
    if (y <= 0) {
      yspeed = random(1, 2);
    }
  }

  void hit() {
    yspeed = random(-1, -1);
    xspeed = random(-2, 2);
  }

  void display() {
    fill(ballColor);
    ellipse(x, y, diameter, diameter);
  }
}

class Paddle {
  float x, y, paddleW, paddleH;

  Paddle(float tempX, float tempY, float tempPaddleW, float tempPaddleH) {
    x = tempX;
    y = tempY;
    paddleW = tempPaddleW;
    paddleH = tempPaddleH;
  }


  void move() {
    //if (keyPressed) {
    if (key == 'a') {
      x -= 2;
    }
    if (key == 'd') {
      x += 2;
    }
    if (x + (paddleW/2) <= 0) {
      x = -(paddleW/2);
    }
    if (x + (paddleW/2) >= width) {
      x = (width -  (paddleW/2));
    }
    //{
  }



  void display() {
    fill(255);
    rect(x, y, paddleW, paddleH);
  }
}



void keyPressed() {
  paddle0.move();
}


I think moving the paddles is one situation where you want to read the key throughout and not only once; try keyPressed as a variable instead of the function keyPressed()

e.g. in draw()

if(keyPressed) {
  paddle0.move();
}
2 Likes