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 › keyPressed problem pressing same key again
Page Index Toggle Pages: 1
keyPressed problem pressing same key again (Read 369 times)
keyPressed problem pressing same key again
May 2nd, 2008, 7:58pm
 
Thanks for helping me with the following.
I´ll bet that everyone (exept me) is able to solve this one...
If I press the Y key the program shall print 1 to the console.
If I press it again it shall print 2. (then 1 , then 2 and so on)
I added a boolean to controll the state of the key (if it has been pressed already or not).
But with my code the programm prints 1 2 just by pressing y one time.

here´s the code:


boolean Ypressed=false;

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


void draw (){

 if (Ypressed == false){    
   if  (keyPressed) {
     if (key == 'y' || key == 'Y'){


       Ypressed = true ;
       println("1");
     }  
   }
 }

 if (Ypressed == true){
   if  (keyPressed) {
     if (key == 'y' || key == 'Y'){


       Ypressed = false;
       println("2");

     }
   }
 }
 println(Ypressed);
}



Thank you for helping me.
le_wuus
Re: keyPressed problem pressing same key again
Reply #1 - May 2nd, 2008, 9:32pm
 
The problem is that you set Ypressed to true, then immediately after it check if it's true, and then set it to false. You need to add "else" in there:

Code:
 void draw (){

if (Ypressed == false){
if (keyPressed) {
if (key == 'y' || key == 'Y'){
Ypressed = true ;
println("1");
}
}
}
else if (Ypressed == true){ //<-------
if (keyPressed) {
if (key == 'y' || key == 'Y'){
Ypressed = false;
println("2");
}
}
}
println(Ypressed);
}
Re: keyPressed problem pressing same key again
Reply #2 - May 2nd, 2008, 11:33pm
 
Thanks mate !
(I just love this community)
Page Index Toggle Pages: 1