# Why won't this run? (keyPressed)

**URL:** https://discourse.processing.org/t/why-wont-this-run-keypressed/18082
**Category:** Coding Questions
**Created:** [February 24, 2020, 9:39pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082 "2020-02-24T21:39:22Z")
**Posts on this page:** 6
**Page:** 2

<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: [February 24, 2020, 11:16pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/21 "2020-02-24T23:16:48Z")

</div>

ok, forget translate

here is a version with working UP and SHIFT

```auto
color c20=color(255);
color c24=color(35, 155, 86);//GREEN
color c21=color(244, 208, 63);//YELLOW
color c22=color(230, 126, 34);//ORANGE
color c27=color(174);//gray

void setup()
{
  //frameRate(10);
  size(500, 500);
}

void draw()
{
  background(200);
  house();
}

// -------------------------------------------------------------------------------------------

void house() {

  translate (-200, -200);

  //else
  //{
  // color c8=color(231,76,60);//RED

  //fill(c8);
  //rect(mouseX+150,mouseY+200,200,180);
  // }
  //}

  int i;
  i=0;
  while (i<5)
  {
    color c34=color(255, 51, 227);
    fill(c34);
    rect(mouseX+150, mouseY+140-33*i, 20, 18);
    i = i+1;
  }

  fill(c20);
  rect(mouseX+150, mouseY+200, 200, 180);

  fill(c24);
  triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);

  // color c3=color(100, 10, 0);
  fill(c27);
  rect(mouseX+234, mouseY+310, 30, 70);

  color c4=color(60, 120, 200);
  fill(c4);
  rect(mouseX+160, mouseY+240, 40, 30);

  color c5=color(60, 120, 200);
  fill(c5);
  rect(mouseX+300, mouseY+240, 40, 30);

  color c6=color(60, 120, 200);
  fill(c6);
  ellipse(mouseX+250, mouseY+255, 70, 50);

  color c7=color(249, 249, 249);
  fill(c7);
  rect(mouseX+247, mouseY+230, 5, 50);

  fill(c7);
  rect(mouseX+215, mouseY+250, 70, 5);

  fill(c7);
  rect(mouseX+160, mouseY+250, 40, 5);

  fill(c7);
  rect(mouseX+300, mouseY+250, 40, 5);

  fill(c7);
  rect(mouseX+317, mouseY+240, 5, 30);

  fill(c21);
  rect(mouseX+177, mouseY+240, 5, 30);
}

// -------------------------------------------------------------------------------------------

void keyPressed() {

  if (key == CODED)
  {
    if (keyCode == UP)
    {
      c20=color(108, 52, 131); //PURPLE
    }
  }

  // rect(mouseX+150, mouseY+200, 200, 180);

  if (keyCode == DOWN)
  {
    c24=color(35, 155, 86);//GREEN

    fill(c24);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }

  if (keyCode == LEFT)
  {
    c21=color(244, 208, 63);//YELLOW

    fill(c21);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
  if (keyCode == RIGHT)
  {
    c22=color(230, 126, 34);//ORANGE
    fill(c22);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
  if (keyCode == SHIFT)
  {
    println("Shift");
    c27=color(174, 214, 241);//BLUE
    fill(c27);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
}
//

```

---

<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: [February 24, 2020, 11:19pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/22 "2020-02-24T23:19:44Z")

</div>

in this version ALL 5 keys change color

you need to do a bit of house cleaning in the code (no pun intended)  
so that all windows use the same color (for example)

Don’t be discouraged!

You can do it!

Chrisir

```auto
color c20=color(255);
color c24=color(222);//
color c21=color(244);//
color c22=color(34);//
color c27=color(174);//gray

void setup()
{
  //frameRate(10);
  size(500, 500);
}

void draw()
{
  background(200);
  house();
}

// -------------------------------------------------------------------------------------------

void house() {

  translate (-200, -200);

  //else
  //{
  // color c8=color(231,76,60);//RED

  //fill(c8);
  //rect(mouseX+150,mouseY+200,200,180);
  // }
  //}

  int i;
  i=0;
  while (i<5)
  {
    color c34=color(255, 51, 227);
    fill(c34);
    rect(mouseX+150, mouseY+140-33*i, 20, 18);
    i = i+1;
  }

  fill(c20);
  rect(mouseX+150, mouseY+200, 200, 180);

  fill(c24);
  triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);

  // color c3=color(100, 10, 0);
  fill(c27);
  rect(mouseX+234, mouseY+310, 30, 70);

  fill(c22);
  rect(mouseX+160, mouseY+240, 40, 30);

  color c5=color(60, 120, 200);
  fill(c5);
  rect(mouseX+300, mouseY+240, 40, 30);

  color c6=color(60, 120, 200);
  fill(c6);
  ellipse(mouseX+250, mouseY+255, 70, 50);

  color c7=color(249, 249, 249);
  fill(c7);
  rect(mouseX+247, mouseY+230, 5, 50);

  fill(c7);
  rect(mouseX+215, mouseY+250, 70, 5);

  fill(c7);
  rect(mouseX+160, mouseY+250, 40, 5);

  fill(c7);
  rect(mouseX+300, mouseY+250, 40, 5);

  fill(c7);
  rect(mouseX+317, mouseY+240, 5, 30);

  fill(c21);
  rect(mouseX+177, mouseY+240, 5, 30);
}

// -------------------------------------------------------------------------------------------

void keyPressed() {

  if (key == CODED)
  {
    if (keyCode == UP)
    {
      c20=color(108, 52, 131); //PURPLE
    }
  }

  // rect(mouseX+150, mouseY+200, 200, 180);

  if (keyCode == DOWN)
  {
    c24=color(35, 155, 86);//GREEN

    fill(c24);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }

  if (keyCode == LEFT)
  {
    c21=color(244, 208, 63);//YELLOW

    fill(c21);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
  if (keyCode == RIGHT)
  {
    c22=color(230, 126, 34);//ORANGE
    // fill(c22);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
  if (keyCode == SHIFT)
  {
    println("Shift");
    c27=color(174, 214, 241);//BLUE
    fill(c27);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
}
//

```

---

<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: [February 24, 2020, 11:20pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/23 "2020-02-24T23:20:41Z")

</div>

It was a problem that the color you changed in keyPressed were not used in house() - there were other colors like c4 used…

😉

---

<div class="post-metadata">

### Author: ![ebec4](https://avatars.discourse-cdn.com/v4/letter/e/91b2a8/32.png) [@ebec4](https://discourse.processing.org/u/ebec4)
#### Post date: [February 24, 2020, 11:22pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/24 "2020-02-24T23:22:42Z")

</div>

Ok thank you so much for all your help.

---

<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: [February 24, 2020, 11:23pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/25 "2020-02-24T23:23:17Z")

</div>

You are welcome!!

Chrisir

---

<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: [February 26, 2020, 1:46pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/26 "2020-02-26T13:46:11Z")

</div>

I placed translate() at the start of house to move all that gets drawn afterwards up and to the left

So the house can be placed everywhere in the window; before that it was too far right and down.

See reference for translate

[Previous page](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082.md?page=1)
