# Processing button on/off

**URL:** https://discourse.processing.org/t/processing-button-on-off/21256
**Category:** Coding Questions
**Created:** [May 24, 2020, 5:07pm UTC](https://discourse.processing.org/t/processing-button-on-off/21256 "2020-05-24T17:07:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rkk16](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@rkk16](https://discourse.processing.org/u/rkk16)
#### Post date: [May 24, 2020, 5:07pm UTC](https://discourse.processing.org/t/processing-button-on-off/21256/1 "2020-05-24T17:07:40Z")

</div>

If on is true, change the background. But what happens is it flickers - I think it’s because it detects multiple mouseclicks and goes on, off, on, off. How do we avoid this? Do I need a delay term? It works, but I only need it to do one switch from on to off or off to on per click (not multiple).

```auto
if (mousePressed & rectOver){
    on = !on;
  }

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [May 24, 2020, 7:49pm UTC](https://discourse.processing.org/t/processing-button-on-off/21256/2 "2020-05-24T19:49:55Z")

</div>

Hi,

When you need to check if the mouse is pressed, I would prefer using the function [`mousePressed()`](https://processing.org/reference/mousePressed_.html) :

```auto
void mousePressed(){
  if(rectOver){
    on = !on;
  }
}

```

---

<div class="post-metadata">

### Author: ![rkk16](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@rkk16](https://discourse.processing.org/u/rkk16)
#### Post date: [May 24, 2020, 8:00pm UTC](https://discourse.processing.org/t/processing-button-on-off/21256/3 "2020-05-24T20:00:15Z")

</div>

Thanks! Works well now, I just included a void mousePressed term within the class and a void mousePressed term out as well which calls the button’s mousepress function.
