# Using multiple mouse functions

**URL:** <https://discourse.processing.org/t/using-multiple-mouse-functions/24485>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 9, 2020, 7:33pm UTC](https://discourse.processing.org/t/using-multiple-mouse-functions/24485 "2020-10-09T19:33:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)\
**Post date:** [October 9, 2020, 7:33pm UTC](https://discourse.processing.org/t/using-multiple-mouse-functions/24485/1 "2020-10-09T19:33:18Z")

</div>

this is what i’m trying to do:

- Create a program that will change the background color based on the mouse.
- You will create a setup(), draw() function. The size of the frame will by 400 by 400.
- **Create a mousePressed() function**. Check to see if the RIGHT or LEFT mouse button has been pressed. Change the background color to red if the LEFT mouse is pressed and blue if the right mouse is pressed.
- **Create a mouseReleased() function.** Change the background to yellow.
- **Create a mouseMoved() function.** Change the background to green.
- **Create a mouseDragged() function**. Change the background to white.

\*\*Can you check my code to see if it works right? \*\*

```auto
void setup()
{
size (400, 400);
}

color c1=color(255, 0, 0);//red, left
color c2=color(0, 0, 255);//blue, right
color c3=color(255, 255, 0);//yellow
color c4=color(0, 255, 0);//green
color c5=color(255, 255, 255);//white

void draw()
{
mousePressed();
    background (0);

  println("mouse was pressed", width, height);
  if(mouseButton ==LEFT)
  {
     background(c1);
    println("and it was the left button", width, height);
  }
  println("mouse was pressed", width, height);
  if(mouseButton ==RIGHT)
  {
    background(c2);
    println("and it was the right button", width, height);
  }
}

void mouseReleased()
{
    background(c3);
}

void mouseMoved()
 {
   background(c4);
}

void mouseDragged()
{
  background(c5);
}

```

---

<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:** [October 9, 2020, 7:38pm UTC](https://discourse.processing.org/t/using-multiple-mouse-functions/24485/2 "2020-10-09T19:38:14Z")

</div>

> [@thughes](#):
>
> \*\*Can you check my code to see if it works right? \*\*

I don’t understand. Can’t you test it??

---

<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:** [October 9, 2020, 7:46pm UTC](https://discourse.processing.org/t/using-multiple-mouse-functions/24485/3 "2020-10-09T19:46:24Z")

</div>

> [@thughes](#):
>
> **Create a mousePressed() function**.

you forgot to do this.

See reference mousePressed(): [mousePressed() / Reference / Processing.org](https://processing.org/reference/mousePressed_.html)

put the if-clauses from draw into mousePressed()

**Remark**

See this is very flickering. This is because you use multiple `background()`

use only ONE background at start of draw(). No other background allowed.

at start of draw() say background (bgcol);

instead of using background everywhere say bgcol=c1; or bgcol=c2; etc.

---

<div class="post-metadata">

**Author:** ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)\
**Post date:** [October 9, 2020, 11:44pm UTC](https://discourse.processing.org/t/using-multiple-mouse-functions/24485/4 "2020-10-09T23:44:07Z")

</div>

I can get all the colors to show…just not the yellow for very long…is there a way to increase the amount of time that the yellow stays on the screen? the yellow is supposed to show when using mouse released

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [October 10, 2020, 1:18am UTC](https://discourse.processing.org/t/using-multiple-mouse-functions/24485/5 "2020-10-10T01:18:45Z")

</div>

`mouseReleased()` is only active for a single frame. If your sketch is running at 60FPS, that’s just 1/60th of a second.

One way you could get the background to appear along is to decrease the frame rate to something lower - if, say, you set the max framerate to 5 FPS, you’ll have a whole 5th of a second to see it before the next frame covers it up.

You’re in luck; you can set the maximum frame rate (aka how often is draw() called every second) with the frameRate() function.

```auto
void setup() {
    size(400, 400);
    frameRate(5);
}

```

---

<div class="post-metadata">

**Author:** ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)\
**Post date:** [October 10, 2020, 1:43am UTC](https://discourse.processing.org/t/using-multiple-mouse-functions/24485/6 "2020-10-10T01:43:18Z")

</div>

Thanks…that was super simple! I knew I need to use frameRate…i just wasn’t sure where to put it!

---

<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:** [October 10, 2020, 1:03pm UTC](https://discourse.processing.org/t/using-multiple-mouse-functions/24485/7 "2020-10-10T13:03:39Z")

</div>

Can you please post your entire Sketch?
