# Help with mousePressed or keyPressed

**URL:** <https://discourse.processing.org/t/help-with-mousepressed-or-keypressed/26689>\
**Category:** Coding Questions\
**Created:** [January 2, 2021, 1:48pm UTC](https://discourse.processing.org/t/help-with-mousepressed-or-keypressed/26689 "2021-01-02T13:48:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PIlw](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@PIlw](https://discourse.processing.org/u/PIlw)\
**Post date:** [January 2, 2021, 1:48pm UTC](https://discourse.processing.org/t/help-with-mousepressed-or-keypressed/26689/1 "2021-01-02T13:48:05Z")

</div>

Hello guys,  
I need help to make this code into mouse pressed or keypressed so I want that the code appears only if I press the mouse or a key as you want but if someone could help me it would be amazing !

```auto
class bug {
  float x = random(width);
  float y = random(-200,-100);
  float speedY = random(4,10);
  float taille = random (50, 70);
  void fall() {
    y = y+speedY;
    
    if (y > height) {
      y = random (-200,-100);
    }
  }
  
  void show() {
    fill(0);
    line(x,y,x,y+taille);
  }
  }
bug[] bugs = new bug[2000];
void setup() {
  fullScreen();
  for (int i = 0; i < bugs.length; i++) {
    bugs[i] = new bug();
  }
}
void draw() {  
  for (int i = 0; i < bugs.length; i++) {
    bugs[i].fall();
    bugs[i].show();
  }
}

```

---

<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:** [January 2, 2021, 2:23pm UTC](https://discourse.processing.org/t/help-with-mousepressed-or-keypressed/26689/2 "2021-01-02T14:23:02Z")

</div>

set a global variable isRunning to false

in keyPressed set it to true

in draw add

```auto

void draw() {
if(isRunning) {
....
}
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 2, 2021, 2:42pm UTC](https://discourse.processing.org/t/help-with-mousepressed-or-keypressed/26689/3 "2021-01-02T14:42:37Z")

</div>

> [@PIlw](#):
>
> I want that the code appears only if I press the mouse or a key

Explore the references:

- [draw() / Reference / Processing.org](https://processing.org/reference/draw_.html) \< Example to increment each frame and uses a global variable. Think about how to adapt this with key or mouse press instead.
- [Variable Scope / Examples / Processing.org](https://processing.org/examples/variablescope.html)
- [keyPressed() / Reference / Processing.org](https://processing.org/reference/keyPressed_.html)
- [mousePressed() / Reference / Processing.org](https://processing.org/reference/mousePressed_.html)
- [boolean / Reference / Processing.org](https://processing.org/reference/boolean.html) \< Sometimes you just want a true or false; instead of a number.
- [if / Reference / Processing.org](https://processing.org/reference/if.html) \< Decision making

Think about the flow:  
Press a key or mouse and set a variable  
Make a decision base on that variable

`:)`

---

<div class="post-metadata">

**Author:** ![PIlw](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@PIlw](https://discourse.processing.org/u/PIlw)\
**Post date:** [January 2, 2021, 4:43pm UTC](https://discourse.processing.org/t/help-with-mousepressed-or-keypressed/26689/4 "2021-01-02T16:43:34Z")

</div>

thank you guys it helped me alot ^^
