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 & HelpPrograms › cannot detect keypressed
Page Index Toggle Pages: 1
cannot detect keypressed (Read 495 times)
cannot detect keypressed
Apr 5th, 2009, 4:41pm
 
Hi all,

I was trying to write a simulation program and want to pause it every single step. Whenever a user presses a specified key, the simulation continues.

I used a while loop to repeat drawing current status and wait a user's key command. Specially, when a user presses the specified key, it will change the value of the expression in while loop. But it seemed that it cannot detect that a key is pressed and cannot leave the loop:(
I don't know why. Please help me out. Thanks!

Here are the related code.

boolean simOn = true;

void keyPressed(){
 println("key pressed"); // even did not print this line
 if( key == ' ' ){
   if(simOn == false){
     simOn = true;
   }
   else{
       simOn = false;
   }
}

void draw(){
  function1();
}

void function1(){
     simOn = false;
     while(!simOn){
       draw_function1();
       // hopefully, a user presses the space, then it leaves this loop
       // but it always loops here
     }
}
Re: cannot detect keypressed
Reply #1 - Apr 5th, 2009, 5:37pm
 
You should take a look at http://processing.org/reference/noLoop_.html
to stop the loop and then simply http://processing.org/reference/redraw_.html on mouseclick...

Page Index Toggle Pages: 1