mousePressed not updating in while loop

Sorry if this questions is a little basic, but I’m tearing my hair out trying to get my code to work. After some troubleshooting i narrowed the offending section to the following snippet:

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

void draw(){
  while (true) {
    if (mousePressed == true) {break;}
  }
  println("OK!!!!");
  delay(10000);
}

I expect that pressing the mouse will break the while loop and print “OK!!!”, but none of that is happening. Turns out mousePressed is not updating to true within the while loop. Anyone know why this is happening?

i am a little bit confused by your question,
because this is not your first processing program,
as you do already arduino, processing, serial communication…

your idea goes so much against any processing thinking
( but also arduino or any PLC like programming )

now in draw:
you draw ( design ) something, but
processing loop execution is like:

  • read mouse, keyboard, ( file lines… ) in a input cycle
  • run code from draw ( and use the input information )
  • do all outputs like
    • send drawing as resulting picture to graphic card output
    • write to files…

and unless you disable it all this is executed 30 times per second
( default frameRate )
IN -> CALC -> OUT .

now for that reason pls

– not use delay in draw
– break while loops by logic ( or not use at all )
( as keyboard and mouse only can be read before draw / while )

1 Like

I’m equally confused by your confusion.

The code snippet is not meant to be complete or even functional, its merely an example to show (what I think is) anomalous behavior. Instead of dumping my entire code here, I’ve done my homework and posted just a summary of my problem. I couldn’t find any documentation about how the mousePressed variable actually updates, which is why i’m posting here.

Now I understand there are probably twenty easier ways to register a mouse click, but for now I’m trying to understand why this particular way doesn’t work.

Hope this clears things up.

  1. You might have guessed, but the delay(10000) is throwaway. It only exists to make execution success (print “OK”) easier for me to observe.
  2. Best coding practices aside, there is no reason why I can’t break a while loop with break. In fact, the following doesn’t work as well:
void draw(){
  while (mousePressed == false) {
    println("Still False");
    delay(100);
  }
  println("Now True");
  delay(10000);
}

Is there documentation somewhere I can refer to for this?

1 Like

possibly you get a feeling about delay when play this:

/* start from example Basics / Input / Mouse Press.  */
boolean usedelay = false;

void setup() {
  size(200,200);
  background(200,200,0);
  println("use key [d] toggle delay 1 sec\nkey [c] clear");
}

void draw() {
  if (mousePressed)  stroke(255);
  else               stroke(0);
  circle(mouseX,mouseY,20);
  if ( usedelay ) delay(1000);
}

void keyPressed() {
 if ( key == 'd' ) {
   usedelay = ! usedelay;
   println("usedelay "+usedelay);
 }
 if ( key == 'c' ) background(200,200,0);      // clear
}

so again, you should not use delay!

but how to delay a action?
for this use must program a timer,
what you can not do as long you get the basic draw loop function wrong.

// add timer
long startT, delayT=5000;

/* start from example Basics / Input / Mouse Press.  */
boolean usedelay = false;

void setup() {
  size(200,200);
  background(200,200,0);
  println("use key [d] toggle delay 1 sec\nkey [c] clear");
}

void draw() {
  if (mousePressed)  stroke(255);
  else               stroke(0);
  circle(mouseX,mouseY,20);
  if ( usedelay ) delay(1000);
  myTimer();
}

void keyPressed() {
 if ( key == 'd' ) {
   usedelay = ! usedelay;
   println("usedelay "+usedelay);
 }
 if ( key == 'c' ) background(200,200,0);      // clear
}

void myTimer(){
  if ( millis() > startT + delayT ) {
   startT += delayT;                    // repeating timer
   println(" what happen now? auto clear ");
   background(200,200,0);
  }
}

Found the solution to my problem.

I had assumed mousePressed works like an interrupt when a button is clicked but it actually polls between each draw() loop for a click, which is why mousePressed never updates within a loop.

My original intention was to send a command to a waiting arduino with a mouse click. Since I can’t while loop indefinitely I replaced the while with an if(waiting state) and it works properly now.

1 Like

no WHILE, very good
what is a ?

if( waiting state )

can you share your working code?

Glad you found a solution.

That is correct – and true for the whole event model. draw() renders the frame, and then then keyboard, mouse, and various post- event updates from any libraries you are using are all processed. frameCount increments, pre-events are called, draw renders again, et cetera. In general, this is why delaying within draw often fails to work as a debugging method – although I have also been down that rabbit hole.

There are some other things that have similar behavior that might be surprising. For example, exit() might surprise you – try running this sketch:

void draw(){
  exit();
  println("hello world");
}

For discussion of that one, see:

1 Like