# mousePressed - not registering just once

**URL:** https://discourse.processing.org/t/mousepressed-not-registering-just-once/19096
**Category:** Coding Questions
**Created:** [March 27, 2020, 10:24pm UTC](https://discourse.processing.org/t/mousepressed-not-registering-just-once/19096 "2020-03-27T22:24:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![benprice](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@benprice](https://discourse.processing.org/u/benprice)
#### Post date: [March 27, 2020, 10:24pm UTC](https://discourse.processing.org/t/mousepressed-not-registering-just-once/19096/1 "2020-03-27T22:24:35Z")

</div>

Hi, I’m very new to processing,

I’m trying to write a piece of code which plays and pauses a video, and want it so that when i press a certain area of the GUI it pauses the video (and displays a pause symbol) and when pressed again it plays it… However I cannot get this to work. It seems to only like to do one or the other, (depending on the placement of code), I think what it is doing is registering once - executing the command, but then registering another click, hence pausing the video. I hope this makes sense (i’m not sure i really understand myself!)

Here is the code:

```auto
void mousePressed(){
  
 if ((pause == true)&& (mousePressed == true) && (mouseX > 540) && (mouseX < 600) && (mouseY > 0) && (mouseY < 45)){
 pause = false;

 println(pause + "1");
} 
 if ((pause == false)&& (mousePressed == true) && (mouseX > 540) && (mouseX < 600) && (mouseY > 0) && (mouseY < 45)){
pause = true;

 println(pause + "2");
 }
}

```

(the println() is there for troubleshooting)

Thanks so much for any help or light you can spread on this for me 🙂

Ben

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [March 27, 2020, 11:45pm UTC](https://discourse.processing.org/t/mousepressed-not-registering-just-once/19096/2 "2020-03-27T23:45:47Z")

</div>

> [@benprice](#):
>
> && (mousePressed == true)

You don’t need this; your code is already running one time per mouse press because it is in mousePressed().

You should either have an if-else structure , or you could return.

```auto
void mousePressed(){
  if( on_button && paused ){
    paused = false;
  } else if ( on_button && !paused ){
    paused = true;
  }
}

```

or

```auto
void mousePressed(){
  if( on_button && paused ){
    paused = false;
    return;
  }
  if ( on_button && !paused ){
    paused = true;
    return;
  }
}

```

Or even just:

```auto
void mousePressed(){
  if( on_button ){
    paused = !paused;
  }
}

```

---

<div class="post-metadata">

### Author: ![benprice](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@benprice](https://discourse.processing.org/u/benprice)
#### Post date: [April 23, 2020, 3:42pm UTC](https://discourse.processing.org/t/mousepressed-not-registering-just-once/19096/3 "2020-04-23T15:42:35Z")

</div>

> [@TfGuy44](#):
>
> uld either have an if-else structure , or you could return.
> 
> ```auto
> void mousePressed(){
> if( on_button && paused ){
> paused = false;
> 
> ```

Amazing!! Thank you so much
