# keyPressed doesn't work

**URL:** <https://discourse.processing.org/t/keypressed-doesnt-work/14406>\
**Category:** Coding Questions\
**Created:** [October 3, 2019, 8:00pm UTC](https://discourse.processing.org/t/keypressed-doesnt-work/14406 "2019-10-03T20:00:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raminl](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@raminl](https://discourse.processing.org/u/raminl)\
**Post date:** [October 3, 2019, 8:00pm UTC](https://discourse.processing.org/t/keypressed-doesnt-work/14406/1 "2019-10-03T20:00:39Z")

</div>

So I’m working on a display full of triangles. I want to use keyPressed to get random colors for each triangle after I press a key, but unfortunately it doesn’t work (not even when I put it in the void draw). Does anyone have a solution?

My code:

```auto
void setup(){
size(800,800);
background(0,0,0);
smooth();
}

int x = 25;
int y = -150;
int count =0;
int value = 0;

void draw(){
  for (int i=0; i <= 16; i++) {
    y += 100;
    x = 25;
    if(count % 2 != 0){
      for (int j = 0; j <= 16; j++) {
        fill(random(0,250), random(0,250), random(0,250));
        triangle(x-25, y, x, y+50, x+25, y);
        fill(random(0,250), random(0,250), random(0,250));
        triangle(x-25, y, x, y-50, x+25, y);
        x += 50;
      }
      count++;
    }
    x = 0;
    if(count % 2 == 0){
      y += 50;
      for (int j = 0; j <= 16; j++) {
        fill(random(0,250), random(0,250), random(0,250));
        triangle(x-25, y, x, y+50, x+25, y);
        fill(random(0,250), random(0,250), random(0,250));
        triangle(x-25, y, x, y-50, x+25, y);
        x += 50;
      }
      count++;
      y -= 50;
    }
   }
}

void keyPressed(){
  if (keyPressed == true) {
    fill(random(0,250), random(0,250), random(0,250));
  }
}

```

**NOTE** : This Processing code belongs to R. Latify, 1st year Industrial Design at TU/e.

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [October 3, 2019, 10:48pm UTC](https://discourse.processing.org/t/keypressed-doesnt-work/14406/2 "2019-10-03T22:48:40Z")

</div>

there are a few problems with your code:

- after the first frame your `x, y,` and `count` variables have already been changed, making your script draw far away from the intended area.  
this can be fixed by resetting your variables at the end of every frame  
you can do this by simply adding the following to the end of your `void draw()`

- after implementing the above, all triangles are assigned a new color every frame, no matter what our keypressed status is.  
this can be fixed by adding an if statement to your void draw that makes it only draw a new frame when a key is pressed

---

<div class="post-metadata">

**Author:** ![raminl](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@raminl](https://discourse.processing.org/u/raminl)\
**Post date:** [October 4, 2019, 1:42pm UTC](https://discourse.processing.org/t/keypressed-doesnt-work/14406/3 "2019-10-04T13:42:06Z")

</div>

Thanks! I want to see a display full of triangles when I run the code, and to change colors when I press a key. But at the moment, when I run the code, I first get a black background. How do I solve this?

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [October 4, 2019, 2:39pm UTC](https://discourse.processing.org/t/keypressed-doesnt-work/14406/4 "2019-10-04T14:39:15Z")

</div>

You could take all the code out of the `if (keypressed)` statement and put it into its own method.  
For example:

```auto
void thing() {
   //put old if statement contents here
} 

```

Then you would call `thing();` in both the `if (keypressed)` statement and `void setup()`

---

<div class="post-metadata">

**Author:** ![raminl](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@raminl](https://discourse.processing.org/u/raminl)\
**Post date:** [October 4, 2019, 5:50pm UTC](https://discourse.processing.org/t/keypressed-doesnt-work/14406/5 "2019-10-04T17:50:09Z")

</div>

Thank you so much for the solution!!

---

<div class="post-metadata">

**Author:** ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)\
**Post date:** [October 4, 2019, 8:54pm UTC](https://discourse.processing.org/t/keypressed-doesnt-work/14406/6 "2019-10-04T20:54:21Z")

</div>

I’m not sure if you still need this but here’s how i solves it

```auto
void setup(){
size(800,800);
background(0,0,0);
smooth();
}

int x = 25;
int y = -150;
int count =0;
int value = 0;

// i added more variables that are colors
// the [1000] means there are 1000 variables
// in red
float[] red = new float[1000];
float[] green = new float[1000];
float[] blue = new float[1000];

int repeat = 0;

void draw(){
// you forgot to reset your variables...
// it happens to the best of us.
x = 25;
y = -150;
count = 0;
value = 0;

for (int i=0; i <= 16; i++) {
y += 100;
x = 25;
if(count % 2 != 0){
for (int j = 0; j <= 16; j++) {

// this repeats over and over resetting the colors
// every time
//fill(random(0,250), random(0,250), random(0,250));

// instead of above we fill with the red green and blue
// variables from above
fill(color(red[j],green[j],blue[j]));
triangle(x-25, y, x, y+50, x+25, y);
//fill(random(0,250), random(0,250), random(0,250));
// the +16 is just for randomness
fill(color(red[j+16],green[j+16],blue[j+16]));
triangle(x-25, y, x, y-50, x+25, y);
x += 50;
}
count++;
}
x = 0;
if(count % 2 == 0){
y += 50;
for (int j = 0; j <= 16; j++) {
//fill(random(0,250), random(0,250), random(0,250));
fill(color(red[j+32],green[j+32],blue[j+32]));
triangle(x-25, y, x, y+50, x+25, y);
//fill(random(0,250), random(0,250), random(0,250));
fill(color(red[j+48],green[j+48],blue[j+48]));
triangle(x-25, y, x, y-50, x+25, y);
x += 50;
}
count++;
y -= 50;
}
}
}

void keyPressed(){

// this fills every triangle the same color
//fill(random(0,250), random(0,250), random(0,250));

repeat = 0;

// will repeat 1000 times assigning all the
// random colors to the variables
while(repeat < 1000){
red[repeat] = random(0,255);
green[repeat] = random(0,255);
blue[repeat] = random(0,255);
repeat = repeat + 1;
}
}

```
