# 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:** 20\
**Page:** 1

<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, 9:39pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/1 "2020-02-24T21:39:23Z")

</div>

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

void draw()
{
	background(200); 
	
	if (keyPressed)
{
     if (key == CODED)
     {
          if (keyCode == UP)
          {
						color c20=color(108,52,131); //PURPLE
fill(c20);
rect(mouseX+150,mouseY+200,200,180);
          }
		 }
}
}
			 
			 
			 	 if (keyCode == DOWN)
          {
						color c24=color(35,155,86);//GREEN
fill(c24);
rect(mouseX+150,mouseY+200,200,180);
					}
			 
			 if (keyCode == LEFT)
          {
						color c21=color(244,208,63);//YELLOW
fill(c21);
rect(mouseX+150,mouseY+200,200,180);
					}
			 if (keyCode == RIGHT)
          {
						color c22=color(230,126,34);//ORANGE
fill(c22);
rect(mouseX+150,mouseY+200,200,180);
					}
			 if (keyCode == SHIFT)
          {
						color c27=color(174,214,241);//BLUE
fill(c27);
rect(mouseX+150,mouseY+200,200,180);
					}
			 
	
			 
			   //else
          //{
              // color c8=color(231,76,60);//RED
//fill(c8);
//rect(mouseX+150,mouseY+200,200,180);
         // }
     //}
	
int i;
	i=0;
	while(i<5)
	{
		if (keypressed)
		{
			
				if (key == 'a')
				{
					color c34=color(255,51,227);
fill(c34);
rect(mouseX+150,mouseY+200,200,180);
				}
				i= i+1;
			 }
		 }

	

	

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

color c2=color(0,100,20);
fill(c2);
triangle(mouseX+130,mouseY+200,mouseX+370,mouseY+200,mouseX+250,mouseY+100);

color c3=color(100,10,0);
fill(c3);
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(c7);
rect(mouseX+177,mouseY+240,5,30);
}

```

---

<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, 10:12pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/2 "2020-02-24T22:12:33Z")

</div>

> [@ebec4](#):
>
> fill(c20);  
> rect(mouseX+150,mouseY+200,200,180);

I guess you have this inside the keyPressed section.

So it gets executed only briefly. You see the rect only short time.

Instead have these lines outside the if-clause.

Only `c20=color(108,52,131); //PURPLE`  
belongs into the if-clause;

Attention: `color c20;` must be placed before setup, so c20 is known outside the if-clause

AND: Welcome to the forum! Good to have your here!

Chrisir

---

<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, 10:21pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/3 "2020-02-24T22:21:58Z")

</div>

Thank you so much for replying. I just started using processing last week so I’m pretty new to it. The purpose of this assignment was to create an object (I did a house) and make it change colors by pressing different keys and also have a while loop somewhere in it. I changed what you said so my code is now what I pasted below, but it’s still not working as intended. Now just a white rectangle appears when I run the code. I don’t know what else to do. If you can help further in any way that’d be great but if not thank you anyway!🙂

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

void draw()  
{  
background(200);

```
if (keyPressed)

```

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

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

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

```

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

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

```

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

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

```

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

int i;  
i=0;  
while(i\<5)  
{  
if (keypressed)  
{

```
			if (key == 'a')
			{
				color c34=color(255,51,227);

```

fill(c34);  
rect(mouseX+150,mouseY+200,200,180);  
}  
i= i+1;  
}  
}

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

color c2=color(0,100,20);  
fill(c2);  
triangle(mouseX+130,mouseY+200,mouseX+370,mouseY+200,mouseX+250,mouseY+100);

color c3=color(100,10,0);  
fill(c3);  
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(c7);  
rect(mouseX+177,mouseY+240,5,30);  
}

---

<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, 10:30pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/4 "2020-02-24T22:30:31Z")

</div>

Okay, I demonstrated what I meant only with the color c20

Have c20 before setup() so that it is global

- **change** it inside the if-clause (which is executed only briefly)

- **use** it outside the if-clause so it is permanent

You need to repeat this for **all** colors!!!

(You had also a small error in the while loop)

```auto

color c20 = color(255); 

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

void draw()
{
  background(200);

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

  // rect(mouseX+150, mouseY+200, 200, 180); // duplicate I think - see below 

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

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

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

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

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

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

  int i;
  i=0;
  while (i<5)
  {
    if (keyPressed)
    {

      if (key == 'a')
      {
        color c34=color(255, 51, 227);

        fill(c34);
        rect(mouseX+150, mouseY+200, 200, 180);
      }
    }
    i= i+1;
  }

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

  color c2=color(0, 100, 20);
  fill(c2);
  triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);

  color c3=color(100, 10, 0);
  fill(c3);
  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(c7);
  rect(mouseX+177, mouseY+240, 5, 30);
}

```

---

<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, 10:38pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/5 "2020-02-24T22:38:58Z")

</div>

Your while loop doesn’t do anything

when you have to use one:

don’t use keyPressed inside

consider to do 5 smoke clouds coming from the chimney like

```auto
    *
   *
  *
 *
 *

```

using a while loop

---

<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, 10:42pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/6 "2020-02-24T22:42:30Z")

</div>

I made changes so now I have this. Is that what you meant? Its still just a white rectangle showing up when I run it.I haven’t done anything to the while loop yet.

color c20=color(108,52,131);  
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,214,241);//BLUE

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

void draw()  
{  
background(200);

```
if (keyPressed)

```

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

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

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

```

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

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

```

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

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

```

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

int i;  
i=0;  
while(i\<5)  
{  
if (keypressed)  
{

```
			if (key == 'a')
			{
				color c34=color(255,51,227);

```

fill(c34);  
rect(mouseX+150,mouseY+200,200,180);  
}  
}  
i= i+1;  
}

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

color c2=color(0,100,20);  
fill(c2);  
triangle(mouseX+130,mouseY+200,mouseX+370,mouseY+200,mouseX+250,mouseY+100);

color c3=color(100,10,0);  
fill(c3);  
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(c7);  
rect(mouseX+177,mouseY+240,5,30);  
}

---

<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, 10:44pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/7 "2020-02-24T22:44:16Z")

</div>

Did you try my code?

You still have if (keypressed) in the while loop

but it must be if (keyPressed) with capital P

---

<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, 10:45pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/8 "2020-02-24T22:45:11Z")

</div>

I see a house

it doesn’t change color when you press UP - my code does

---

<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, 10:47pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/9 "2020-02-24T22:47:23Z")

</div>

As I mentioned:

when you say color c24… before setup() ( color c24=color(35, 155, 86);//GREEN )

DO NOT say color c24=… later !!!

instead just say c24 = …

when repeating the word **color** , you declare a new variable which is overshadowing the first one!!!

**In this version, we make use of more functions**

but you still have to order the colors, c20 is done

- and no rects should be in function keyPressed and only color changes in keyPressed

- all color uses and rects in function house()

```auto

color c20 = color(255); 

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

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

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

void house() {

  //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);

  color c2=color(0, 100, 20);
  fill(c2);
  triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);

  color c3=color(100, 10, 0);
  fill(c3);
  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(c7);
  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)
  {
    color c24=color(35, 155, 86);//GREEN

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

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

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

```

---

<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, 10:54pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/10 "2020-02-24T22:54:18Z")

</div>

Okay so now I have this but its staying purple and not changing color with the press of any keys

color c20=color(108,52,131);  
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,214,241);//BLUE

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

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

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

void house() {

//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);

color c2=color(0, 100, 20);  
fill(c2);  
triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);

color c3=color(100, 10, 0);  
fill(c3);  
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(c7);  
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)  
{  
color c24=color(35, 155, 86);//GREEN

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

```

}

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

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

```

}  
if (keyCode == RIGHT)  
{  
color c22=color(230, 126, 34);//ORANGE  
fill(c22);  
rect(mouseX+150, mouseY+200, 200, 180);  
}  
if (keyCode == SHIFT)  
{  
color 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, 10:57pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/11 "2020-02-24T22:57:07Z")

</div>

As I mentioned:

when you say color c24… before setup() ( color c24=color(35, 155, 86);//GREEN )

DO NOT say color c24=… later !!!

instead just say c24 = …

when repeating the word **color** , you declare a new variable which is overshadowing the first one!!!

**Remark**

- **NO** rects should be in function keyPressed and only color changes in keyPressed

- all color uses and rects in function house()

---

<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:00pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/12 "2020-02-24T23:00:40Z")

</div>

Like this?

color c20=color(108,52,131);  
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,214,241);//BLUE

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

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

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

void house() {

//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);

color c2=color(0, 100, 20);  
fill(c2);  
triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);

color c3=color(100, 10, 0);  
fill(c3);  
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(c7);  
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)  
{  
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:01pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/13 "2020-02-24T23:01:39Z")

</div>

Why do you ask me?

Does it work?

Does it do what you expect?

---

<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:07pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/14 "2020-02-24T23:07:51Z")

</div>

Puuh…

so, when you want the keys to CHANGE the color, don’t give the colors initially the SAME values they get when you press the key… (then you won’t see a change)

say `color c20=color(255);` before setup - WHITE - so it gets purple when you hit UP

c27 : `color c27=color(174);//gray` before setup()

**REMARK**

BUT c27 NEVER gets used… try to use it somwhere…

same for the other colors

Chrisir

---

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

</div>

Ok Im sorry, I am asking you because I am very new at this and I don’t understand half of what you’re saying. I’m trying to do what you’re saying but nothing is changing.Thank you for your help but I just give up on this.

---

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

</div>

hang on…

I can give you a kick start here

😉

and implement also translate (-200, -200); at start of house();

---

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

</div>

I just don’t even know what translate means

---

<div class="post-metadata">

**Author:** ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)\
**Post date:** [February 24, 2020, 11:14pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/18 "2020-02-24T23:14:05Z")

</div>

Try checking out this tutorial on 2D movements [https://processing.org/tutorials/transform2d/](https://processing.org/tutorials/transform2d/)

---

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

</div>

so the basic idea is simple:

- The function `keyPressed()` gets excuted only briefly, during the key is down. So no use drawing rects here. Or using fill. Instead we just change the colors (which are stored) to have a **permanent** effect. Like: `c27=color(174, 214, 241);//BLUE` for example.
- house() gets executed throughout (it’s called from draw()!). Good. We want to draw rects here using the colors with fill(c20) for example.
- To have the same colors in keyPressed() as in house() we need to declare them **globally**. That’s before setup(). They are known in all functions. But declare them with _another_ color than the color we are going to change them to in keyPressed() so we see a _difference_.

---

<div class="post-metadata">

**Author:** ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)\
**Post date:** [February 24, 2020, 11:14pm UTC](https://discourse.processing.org/t/why-wont-this-run-keypressed/18082/20 "2020-02-24T23:14:49Z")

</div>

Think of the screen as a grid. Instead of moving objects across the grid, you can just move the grid itself! That is basically what translate does. It can be very useful in many situations.

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