# When do you use keyPressed as function?

**URL:** <https://discourse.processing.org/t/when-do-you-use-keypressed-as-function/22278>\
**Category:** Beginners\
**Created:** [June 30, 2020, 9:02am UTC](https://discourse.processing.org/t/when-do-you-use-keypressed-as-function/22278 "2020-06-30T09:02:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![klap](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@klap](https://discourse.processing.org/u/klap)\
**Post date:** [June 30, 2020, 9:02am UTC](https://discourse.processing.org/t/when-do-you-use-keypressed-as-function/22278/1 "2020-06-30T09:02:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [June 30, 2020, 10:08am UTC](https://discourse.processing.org/t/when-do-you-use-keypressed-as-function/22278/2 "2020-06-30T10:08:28Z")

</div>

Hi,

Looking at the [Processing documentation](https://processing.org/reference), 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 :

```auto
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 :

```auto
if (keyPressed) { ...

```

---

<div class="post-metadata">

**Author:** ![klap](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@klap](https://discourse.processing.org/u/klap)\
**Post date:** [July 16, 2020, 1:28pm UTC](https://discourse.processing.org/t/when-do-you-use-keypressed-as-function/22278/3 "2020-07-16T13:28:56Z")

</div>

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

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [July 16, 2020, 2:27pm UTC](https://discourse.processing.org/t/when-do-you-use-keypressed-as-function/22278/4 "2020-07-16T14:27:54Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![klap](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@klap](https://discourse.processing.org/u/klap)\
**Post date:** [July 16, 2020, 2:39pm UTC](https://discourse.processing.org/t/when-do-you-use-keypressed-as-function/22278/5 "2020-07-16T14:39:17Z")

</div>

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.

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 16, 2020, 3:52pm UTC](https://discourse.processing.org/t/when-do-you-use-keypressed-as-function/22278/6 "2020-07-16T15:52:53Z")

</div>

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()

```auto
if(keyPressed) {
  paddle0.move();
}

```
