keyPressed Help

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

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

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/

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

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);
}
2 Likes

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

1 Like

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

Why frameRate(1) ?

1 Like

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

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

Delete this from draw()

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

1 Like

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



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);
}
//
1 Like

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

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

1 Like

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