Why is this keyPressed not working

Inside the draw function i made a keyPressed. I want that piece of code to run only if i press r, but for some reason it is not working. Is there a better way to do it. I also want to put multiple different keys to run different part of the code( not included in the below code). What is the best way to do it. Please help

if (keyPressed) {
    if (key=='r') {

      shape(baseMap, 10, 10, width, height);


      for (int i=1; i<intConfirmedCases.size(); i++) {

        fill(#F21642);
        int x=intX.get(i); //random value of x position
        int y= intY.get(i); //random value of y position
        ellipse(x, y, 30, 30); 
        //If condition was used to align the text inside the ellipse. For value of x is changed bases the the number of digits in the confirmed case value so that the text is perfectly inside the ellipse
        if (intConfirmedCases.get(i)<10) {
          fill(#0D0C0C);
          text(intConfirmedCases.get(i), x-3, y+2);
        } //Confirmedcases is printed insde the ellipse
        if (intConfirmedCases.get(i)<100&&intConfirmedCases.get(i)>=10) {
          fill(#0D0C0C);
          text(intConfirmedCases.get(i), x-7, y+2);
        }
        if (intConfirmedCases.get(i)>99) {
          fill(#0D0C0C);
          text(intConfirmedCases.get(i), x-10, y+2);
        }

        if (strDistrict.get(i).equals("parsa")||
          strDistrict.get(i).equals("bara")||
          strDistrict.get(i).equals("rautahat")||
          strDistrict.get(i).equals("sarlahi")||
          strDistrict.get(i).equals("mahottari")||
          strDistrict.get(i).equals("dhanusha")) {
          pushMatrix();
          textSize(10);
          fill(#070808);
          //translate(x+15,y+10);
          //rotate(PI/4);
          text(strDistrict.get(i), x-15, y+30);
          popMatrix();
        } else {    
          textSize(10);
          fill(#373739);
          text(strDistrict.get(i), x+15, y+10);
        }



        text("Index", 30, height-130);
        fill(#F21642);
        ellipse(40, height-100, 30, 30);
        fill(#0E0F0E);
        text("--> Confirmed Corona Cases", 70, height-97);
      }
    }
  }
}

Especially when you have background() at the start of draw() everything you do in keyPressed() would not be permanent

To make it permanent please set an int variable to a certain value. Name the variable command.

In draw() evaluate command with if(command){…} and act accordingly

  • Now it’s permanent

command can have different values for different actions: 0,1,2…

Chrisir

Hey chrisir,
I am sorry, but can you give me an example for it. I didn’t quite get your answer. Sorry for your trouble

  • Before setup() say int command = 0;

  • in keyPressed() say command=1; for key r, command = 2; for key c etc.

  • in draw say if(command==1){ what you do in keyPressed now do here instead }

Chrisir

 if (command==1) {


    shape(baseMap, 10, 10, width, height);


    for (int i=1; i<intConfirmedCases.size(); i++) {

      fill(#F21642);
      int x=intX.get(i); //random value of x position
      int y= intY.get(i); //random value of y position
      ellipse(x, y, 30, 30); 
      //If condition was used to align the text inside the ellipse. For value of x is changed bases the the number of digits in the confired case value so that the text is perfectly inside the ellipse
      if (intConfirmedCases.get(i)<10) {
        fill(#0D0C0C);
        text(intConfirmedCases.get(i), x-3, y+2);
      } //Confirmedcases is printed insde the ellipse
      if (intConfirmedCases.get(i)<100&&intConfirmedCases.get(i)>=10) {
        fill(#0D0C0C);
        text(intConfirmedCases.get(i), x-7, y+2);
      }
      if (intConfirmedCases.get(i)>99) {
        fill(#0D0C0C);
        text(intConfirmedCases.get(i), x-10, y+2);
      }

      if (strDistrict.get(i).equals("parsa")||
        strDistrict.get(i).equals("bara")||
        strDistrict.get(i).equals("rautahat")||
        strDistrict.get(i).equals("sarlahi")||
        strDistrict.get(i).equals("mahottari")||
        strDistrict.get(i).equals("dhanusha")) {
        pushMatrix();
        textSize(10);
        fill(#070808);
        //translate(x+15,y+10);
        //rotate(PI/4);
        text(strDistrict.get(i), x-15, y+30);
        popMatrix();
      } else {    
        textSize(10);
        fill(#373739);
        text(strDistrict.get(i), x+15, y+10);
      }



      text("Index", 30, height-130);
      fill(#F21642);
      ellipse(40, height-100, 30, 30);
      fill(#0E0F0E);
      text("--> Confirmed Corona Cases", 70, height-97);
    }
  }
}

void keyPressed() {
  if (key=='r') {
    command=1;
  }
}

I did it, but it is not rendering anything when i press r.

Edit: it worked now. I had noLoop() on the setup before. Thankyou for your help

1 Like

ah, noLoop() is a pain…

in the forum, this variable “command” is mostly named state (or screen), although it’s probably not exactly the same

here is an example

Chrisir

int command=0; 

void setup() {
  size(650, 555);
}

void draw() {
  background(0); 

  if (command==1) {
    rect(11, 11, 33, 333);
  } else if (command==2) {
    ellipse(100, 110, 33, 33);
  }
}

void keyPressed() {
  if (key=='r') {
    command=1;
  } else if (key=='c') {
    command=2;
  }
}
1 Like