We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › waiting for user input
Page Index Toggle Pages: 1
waiting for user input (Read 1130 times)
waiting for user input
Jan 25th, 2010, 3:48pm
 
I'm having trouble getting my code to wait for some form of user input, either a mouse click or key press. I've seen others using noLoop() and loop(), though these appear to complete the code in draw() before pausing. I want my code to pause in one spot until the user inputs something. I've tried setting up while loops with a condition which should be met if the user presses the mouse but this seems to get into an infinite loop, as though the mousePress is never true (see code below).

If anyone has any thoughts on how to do this, I would be very thankful.

Code:
boolean cont = false;

void setup(){
   size(256, 256);  // Stage size
}

void draw(){
   println("start");
   while(cont == false){
     delay(100);
     if(mousePressed){
       cont=true;
     }
   }
  println("done");
}    
Re: waiting for user input
Reply #1 - Jan 25th, 2010, 4:03pm
 
Re: waiting for user input
Reply #2 - Jan 26th, 2010, 1:32am
 
jayL wrote on Jan 25th, 2010, 3:48pm:
Code:
boolean cont = false;

void setup(){
   size(256, 256);  // Stage size
}

void draw(){
   println("start");
   while(cont == false){
     delay(100);
     if(mousePressed){
       cont=true;
     }
   }
  println("done");
}    


That's a bad approach for many reasons.  The main one is the while loop - you need to be very careful when you use these otherwise you can easily get into an infinite loop and crash your sketch.  You also appear to be 'fighting the draw loop': "I want my code to pause in one spot".

The problem is that if you 'pause' the code, using while loops, noLoop() etc. it's no longer registering input: input events are processed after the draw loop completes.  draw loop paused - no input registered.  That's why your mousePressed isn't registered.

In actual fact as well as using the methods suggested by TfGuy44 you were almost there.  Rather than 'pausing' you need to think more along the lines of 'skipping over' anything you don't want to happen yet:

Code:
boolean cont = false;

void setup(){
   size(256, 256);  // Stage size
}

void draw(){
   // use a condition not a loop
   if(cont == true){
     // I don't want this to happen yet
     println("mouse was pressed");
   }

   if(mousePressed){
       cont=true;
   }

}

// the separate methods approach has some advantages
// e.g. separates user input from draw = potentially greater
// clarity of code; and also handles more events
void mouseReleased() {
 cont = false;
}
Re: waiting for user input
Reply #3 - Jan 26th, 2010, 10:20am
 
Thanks blindfish,

I knew the while loop was a bad approach but I didn't understand why the mousePress wasn't registering. I had assumed that this variable was being updated in an interrupt. Knowing that it isn't updated until draw() completes explains my confusion.

Thanks also for the idea of 'skipping'.
Page Index Toggle Pages: 1