# P5 'mousePressed()' function error

**URL:** <https://discourse.processing.org/t/p5-mousepressed-function-error/43221>\
**Category:** p5.js\
**Created:** [November 16, 2023, 1:06am UTC](https://discourse.processing.org/t/p5-mousepressed-function-error/43221 "2023-11-16T01:06:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [November 16, 2023, 1:06am UTC](https://discourse.processing.org/t/p5-mousepressed-function-error/43221/1 "2023-11-16T01:06:52Z")

</div>

p5 ‘mousePressed()’ function error

hello. Nice to meet you.  
Please refer to the video.

[https://www.youtube.com/shorts/n2i0YRm-yJY](https://www.youtube.com/shorts/n2i0YRm-yJY)

1. WEB was implemented through ‘p5’.
2. Image change was performed through ‘mousePressed()’.
3. On PC, ‘mousePressed()’ is called once per touch.
4. On smartphones, ‘mousePressed()’ is called multiple times with a single touch.

What’s wrong with smartphones? Is there any solution?

As of now, I have implemented it as follows.

> var SAFE\_PRESS = 0;  
> function mousePressed() {  
> if(SAFE\_PRESS \< millis()){  
> SAFE\_PRESS = millis() + 1000;  
> }  
> }

Can you tell me a good solution or cause other than the above?

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [November 16, 2023, 3:46am UTC](https://discourse.processing.org/t/p5-mousepressed-function-error/43221/2 "2023-11-16T03:46:55Z")

</div>

Hi  
Try this

```auto
let mouseCounter = 0;
function mousePressed() {
    mouseCounter++;
    return false;
}

```

> **[Creative Coding 1, MART 120](https://montana-media-arts.github.io/creative-coding-1/modules/week-7/more-mouse-events/)**
>
> A course to teach code so that it becomes a tool for art and creativity.

> <https://github.com/processing/p5.js/issues/1811#issue-208648774>
>
> Everything was working fine for a while with a program I have until Chrome was r…ecently updated to v56. On the desktop, it still seems to work fine, but if you're on mobile or use the emulator in the browser, there seems to be an issue with mousePressed. 
> 
> When I press anywhere on the canvas, I get the following in the console:
> 
> Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080
> 
> Uncaught TypeError: Cannot read property 'clientX' of undefined
> at d (p5.min.js:5)
> at e.\_updateNextMouseCoords (p5.min.js:5)
> at e.\_ontouchend (p5.min.js:6)
> 
> I do return false at the end of the mousePressed function. If I comment that out, I no longer receive the warning about preventDefault but I still get the Uncaught TypeError.

 ![Screenshot_2023-11-16-06-49-19-546](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/8/b84f3b42f3fc65ba93431502b2d14f12c01db6a3.jpeg)

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [November 16, 2023, 7:20am UTC](https://discourse.processing.org/t/p5-mousepressed-function-error/43221/3 "2023-11-16T07:20:21Z")

</div>

@jafal

Hello. how are you?

Thank you for quick response. Thank you for caring me.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [November 16, 2023, 5:55pm UTC](https://discourse.processing.org/t/p5-mousepressed-function-error/43221/4 "2023-11-16T17:55:40Z")

</div>

> [@GWAK](#):
>
> Hello. how are you?
> 
> Thank you for quick response. Thank you for caring me.

Hi  
I am fine what’s about you hope you fine too

Here is the p5.js mouse issue for touch devices from the link I provided

Please be advised: per the p5 reference page, that some browsers have default behavior for `mousePressed()` functions. “Browsers may have different default behaviors attached to various mouse events. To prevent any default behavior for this event, add `return false` to the end of the method.” JUST DO IT!

Note when you remove this line  
return false;  
Counting won’t be stable
