# Why is this keyPressed not working

**URL:** https://discourse.processing.org/t/why-is-this-keypressed-not-working/22603
**Category:** Coding Questions
**Created:** [July 14, 2020, 8:09am UTC](https://discourse.processing.org/t/why-is-this-keypressed-not-working/22603 "2020-07-14T08:09:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DeathEater](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@DeathEater](https://discourse.processing.org/u/DeathEater)
#### Post date: [July 14, 2020, 8:09am UTC](https://discourse.processing.org/t/why-is-this-keypressed-not-working/22603/1 "2020-07-14T08:09:59Z")

</div>

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

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

```

---

<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: [July 14, 2020, 8:15am UTC](https://discourse.processing.org/t/why-is-this-keypressed-not-working/22603/2 "2020-07-14T08:15:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DeathEater](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@DeathEater](https://discourse.processing.org/u/DeathEater)
#### Post date: [July 14, 2020, 8:32am UTC](https://discourse.processing.org/t/why-is-this-keypressed-not-working/22603/3 "2020-07-14T08:32:15Z")

</div>

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

---

<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: [July 14, 2020, 8:46am UTC](https://discourse.processing.org/t/why-is-this-keypressed-not-working/22603/4 "2020-07-14T08:46:29Z")

</div>

- 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

---

<div class="post-metadata">

### Author: ![DeathEater](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@DeathEater](https://discourse.processing.org/u/DeathEater)
#### Post date: [July 14, 2020, 9:06am UTC](https://discourse.processing.org/t/why-is-this-keypressed-not-working/22603/5 "2020-07-14T09:06:33Z")

</div>

```auto
 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

---

<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: [July 14, 2020, 12:35pm UTC](https://discourse.processing.org/t/why-is-this-keypressed-not-working/22603/6 "2020-07-14T12:35:49Z")

</div>

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

```auto
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;
  }
}

```
