Multiple mouse clicks issue

How do I consider multiple mouse clicks as one mouse click?
I have an if condition which checks for mouse click condition and increments the counter variable, but when mouse is kept pressed for a long time, there are multiple mouse clicks but I want the counter variable to increment only by one.

when you use the function mousePressed() as opposed to the boolean variable mousePressed it’s better

see reference

  • function mousePressed() is with brackets ()
  • the boolean variable mousePressed is without the brackets ()

this is the code. What change do I do here?

if (mousePressed and mouseClicked and mouseX > 500 and mouseX < 600 and mouseY > 500 and mouseY < 600):
               count+=1

when you use the function mousePressed() as opposed to the boolean variable mousePressed it’s better

see reference

when i replace mousePressed with mousePressed() in if condition, it shows an error saying bool object not callable.

here is the reference for mousePressed() - the function

see https://www.processing.org/reference/mousePressed_.html

still I want it to count only once in that particular region/area.

Does it work now?

Please show your entire running code to get real help.

I suppose you wrote the condition you posted inside the draw() function. It is then evaluated every time the draw function is called.
I guess that what you want is to write the condition inside the mouseClicked() function as Chrisir said. It would look like so:

void mouseClicked() {
   if(mouseX > 500 & mouseX < 600 && mouseY > 500 && mouseY < 600) count += 1;
}

The mouseClicked function is automatically called once for each time the mouse is pressed and then released.

1 Like

Yeah, and the mousePressed () function too.

mousePressed as a boolean variable registers multiple times


int count = 0; 

void setup() {
  size(600, 600);
  background(0);
}

void draw() {
  background(0); 
  text(count, 3, 13);

  if (mousePressed && 
    mouseX > 500 && 
    mouseX < 600 &&
    mouseY > 500 &&
    mouseY < 600)
    count+=1;
}


mousePressed() as a function registers only ONCE


int count = 0; 

void setup() {
  size(600, 600);
  background(0);
}

void draw() {
  background(0); 
  text(count, 3, 13);
}

//----------------------------------------------------------
// Input functions 

void mousePressed() {
  if ( mouseX > 500 && 
    mouseX < 600 &&
    mouseY > 500 &&
    mouseY < 600)
    count+=1;
}