# keyPressed Help

**URL:** <https://discourse.processing.org/t/keypressed-help/14807>\
**Category:** Beginners\
**Created:** [October 19, 2019, 8:26pm UTC](https://discourse.processing.org/t/keypressed-help/14807 "2019-10-19T20:26:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![TandemMISMI](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@TandemMISMI](https://discourse.processing.org/u/TandemMISMI)\
**Post date:** [October 19, 2019, 8:26pm UTC](https://discourse.processing.org/t/keypressed-help/14807/1 "2019-10-19T20:26:20Z")

</div>

Hello all , I just begin with processing few days ago and I need help about the function keyPressed  
I want to change the position of my man’s arms when I press a key but when I press the key I have the position before and the one at the same time  
thanks

```auto
float bras2 = 305;
if (keyPressed == true ) {
  bras2 =255;
}
fill(#E3DBC3);
rectMode(CENTER);
rect(365, bras2, 10, 80);
rectMode(CENTER);
rect(235, bras2, 10, 80);

```

---

<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:** [October 20, 2019, 9:49pm UTC](https://discourse.processing.org/t/keypressed-help/14807/2 "2019-10-20T21:49:45Z")

</div>

Is this your entire code?

well to have a animation you need to have setup() and draw() - see the first text tutorials

E.g.: The section Hello mouse here: [https://www.processing.org/tutorials/overview/](https://www.processing.org/tutorials/overview/)

also, it helps to have background at the beginning of draw()

Also repeat `bras2 = 305;` because it won’t go back to its initial value automatically

```auto
float bras2 = 305;

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

void draw() {

  background(0);

  if (keyPressed == true ) {
    bras2 = 255;
  } else {
    bras2 = 305;
  }
  fill(#E3DBC3);
  rectMode(CENTER);
  rect(365, bras2, 10, 80);
  rectMode(CENTER);
  rect(235, bras2, 10, 80);
}

```

---

<div class="post-metadata">

**Author:** ![TandemMISMI](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@TandemMISMI](https://discourse.processing.org/u/TandemMISMI)\
**Post date:** [October 21, 2019, 6:54pm UTC](https://discourse.processing.org/t/keypressed-help/14807/3 "2019-10-21T18:54:59Z")

</div>

Thank you for your help here is the complete code that must be pretty ugly  
When I press a key I would like the variable to be applied but once the key is released that the default value comes back but here I have the two remaining

> void setup() {  
> size(600, 400);  
> background(255);  
> }  
> void draw() {  
> fill(#E3DBC3);  
> rectMode(CENTER);  
> rect(300, 200, 100, 100);  
> fill(255, 255, 255);  
> //bouche  
> pushStyle();  
> float bouche1 =0;  
> float bouche2 =-PI;  
> float boucheH =230;  
> if (keyPressed == true ) {  
> bouche1 = PI;  
> bouche2 = 0;  
> boucheH = 220;  
> }  
> fill(#F0D7ED);  
> arc(300, boucheH, 20, 22, bouche2, bouche1, CHORD);  
> popStyle();  
> //cheveux  
> pushStyle();  
> fill(63, 0, 0);  
> beginShape();  
> vertex(250, 150);  
> vertex(350, 150);  
> vertex(350, 175);  
> vertex(330, 175);  
> vertex(330, 165);  
> vertex(270, 165);  
> vertex(270, 175);  
> vertex(250, 175);  
> endShape(CLOSE);  
> popStyle();
> 
> //tronc  
> pushStyle();  
> color aColor = color( random(255),random(255),random(255) );  
> frameRate(1);  
> if (mousePressed == true) {  
> fill(aColor);  
> } else {  
> fill(0, 0, 0);  
> }  
> rectMode(CENTER);  
> rect(300, 310, 100, 100);  
> popStyle();  
> // yeux  
> fill(0, 0, 0);  
> ellipseMode(CENTER);  
> ellipse(275, 195, 8, 12);  
> ellipseMode(CENTER);  
> ellipse(325, 195, 8, 12);
> 
> //cravattes et cou  
> pushStyle();  
> fill(#E3DBC3);  
> triangle(270, 260, 330, 260, 300, 340);  
> triangle(300, 250, 295, 260, 305, 260);  
> fill(255, 0, 0);  
> triangle(300, 305, 290, 275, 310, 275);  
> triangle(300, 260, 290, 270, 310, 270);  
> popStyle();  
> //boutons  
> pushStyle();  
> fill(255, 255, 255);  
> ellipseMode(CENTER);  
> ellipse(290, 265, 8, 8);  
> ellipseMode(CENTER);  
> ellipse(310, 265, 8, 8);  
> ellipseMode(CENTER);  
> ellipse(300, 347, 8, 8);  
> popStyle();  
> //bras  
> float bras2 = 305;  
> if (keyPressed == true ) {  
> bras2 = 255;  
> } else {  
> bras2 = 305;  
> }  
> fill(#E3DBC3);  
> rectMode(CENTER);  
> rect(365, bras2, 10, 80);  
> rectMode(CENTER);  
> rect(235, bras2, 10, 80);  
> //epaules  
> ellipseMode(CENTER);  
> ellipse(245, 275, 10, 20);  
> ellipseMode(CENTER);  
> ellipse(355, 275, 10, 20);  
> }

---

<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:** [October 21, 2019, 7:01pm UTC](https://discourse.processing.org/t/keypressed-help/14807/4 "2019-10-21T19:01:26Z")

</div>

say background(255);  
at start of draw()

Why `frameRate(1)` ?

---

<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:** [October 21, 2019, 7:08pm UTC](https://discourse.processing.org/t/keypressed-help/14807/5 "2019-10-21T19:08:36Z")

</div>

> [@TandemMISMI](#):
>
> color aColor = color( random(255),random(255),random(255) );  
> frameRate(1);  
> if (mousePressed == true) {  
> fill(aColor);  
> } else {  
> fill(0, 0, 0);  
> }

to avoid the fast change of colors make aColor a global variable (define it before setup()) and say in a new function mousePressed() :

```auto
void mousePressed() {
       aColor = color( random(255),random(255),random(255) );
}

```

Delete this from draw()

`color aColor = color( random(255),random(255),random(255) );`

---

<div class="post-metadata">

**Author:** ![TandemMISMI](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@TandemMISMI](https://discourse.processing.org/u/TandemMISMI)\
**Post date:** [October 21, 2019, 7:57pm UTC](https://discourse.processing.org/t/keypressed-help/14807/6 "2019-10-21T19:57:25Z")

</div>

Hello thank you for your help I can not do I have several more error message ( unexpected token: void  
expecting EOF, found ‘else’)  
and this part of the code does not pose a problem is especially the position of the arms that jam because I have assigned two positions that vary in if a key is pressed but they appear at the same time

---

<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:** [October 21, 2019, 8:08pm UTC](https://discourse.processing.org/t/keypressed-help/14807/7 "2019-10-21T20:08:35Z")

</div>

```auto

void setup() {
  size(600, 400);
  background(255);
}

void draw() {

  background(255);

  fill(#E3DBC3);
  rectMode(CENTER);
  rect(300, 200, 100, 100);
  fill(255, 255, 255);

  //bouchef
  pushStyle();
  float bouche1 =0;
  float bouche2 =-PI;
  float boucheH =230;
  if (keyPressed == true ) {
    bouche1 = PI;
    bouche2 = 0;
    boucheH = 220;
  }
  fill(#F0D7ED);
  arc(300, boucheH, 20, 22, bouche2, bouche1, CHORD);
  popStyle();

  //cheveux
  pushStyle();
  fill(63, 0, 0);
  beginShape();
  vertex(250, 150);
  vertex(350, 150);
  vertex(350, 175);
  vertex(330, 175);
  vertex(330, 165);
  vertex(270, 165);
  vertex(270, 175);
  vertex(250, 175);
  endShape(CLOSE);
  popStyle();

  //tronc
  pushStyle();
  color aColor = color( random(255), random(255), random(255) );
  //frameRate(1);
  if (mousePressed == true) {
    fill(aColor);
  } else {
    fill(0, 0, 0);
  }
  rectMode(CENTER);
  rect(300, 310, 100, 100);
  popStyle();

  // yeux
  fill(0, 0, 0);
  ellipseMode(CENTER);
  ellipse(275, 195, 8, 12);
  ellipseMode(CENTER);
  ellipse(325, 195, 8, 12);

  //cravattes et cou
  pushStyle();
  fill(#E3DBC3);
  triangle(270, 260, 330, 260, 300, 340);
  triangle(300, 250, 295, 260, 305, 260);
  fill(255, 0, 0);
  triangle(300, 305, 290, 275, 310, 275);
  triangle(300, 260, 290, 270, 310, 270);
  popStyle();

  //boutons
  pushStyle();
  fill(255, 255, 255);
  ellipseMode(CENTER);
  ellipse(290, 265, 8, 8);
  ellipseMode(CENTER);
  ellipse(310, 265, 8, 8);
  ellipseMode(CENTER);
  ellipse(300, 347, 8, 8);
  popStyle();

  //bras
  float bras2 = 305;
  if (keyPressed == true ) {
    bras2 = 255;
  } else {
    bras2 = 305;
  }
  fill(#E3DBC3);
  rectMode(CENTER);
  rect(365, bras2, 10, 80);
  rectMode(CENTER);
  rect(235, bras2, 10, 80);

  //epaules
  ellipseMode(CENTER);
  ellipse(245, 275, 10, 20);
  ellipseMode(CENTER);
  ellipse(355, 275, 10, 20);
}
//

```

---

<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:** [October 21, 2019, 8:45pm UTC](https://discourse.processing.org/t/keypressed-help/14807/8 "2019-10-21T20:45:06Z")

</div>

> [@TandemMISMI](#):
>
> but they appear at the same time

that was because you didn’t have a `background()` at the beginning of `draw()`.

---

<div class="post-metadata">

**Author:** ![TandemMISMI](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@TandemMISMI](https://discourse.processing.org/u/TandemMISMI)\
**Post date:** [October 21, 2019, 8:53pm UTC](https://discourse.processing.org/t/keypressed-help/14807/9 "2019-10-21T20:53:52Z")

</div>

Effectively I added the background to my base code and it works as I wanted  
but your formatting code is more clearly  
Thanks again for the tips

---

<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:** [October 21, 2019, 8:54pm UTC](https://discourse.processing.org/t/keypressed-help/14807/10 "2019-10-21T20:54:46Z")

</div>

You can get auto-format by hitting **Ctrl-t** in processing prior to posting
