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 › a probably stupid question about keyPressed
Page Index Toggle Pages: 1
a probably stupid question about keyPressed (Read 684 times)
a probably stupid question about keyPressed
May 10th, 2010, 11:25pm
 
Hi,

I am looking for a way to only detect 1 keyPress, instead of repeating ones, I guess it has to do with logic, but I can't to think that logically, anyone any tips?

here's the very basic code I'm using:

color[] colors={#FF0000,#FF6205,#FFAB01,#FDDF00,#FFFF00,#90F900,#00B511,#00D3FD,#0035FD,
#2B00B3,#6E00CC,#E100E6};
color c=colors[0];
 int index=0;

void setup(){
 size(400,400);
 noStroke();
 background(255);
}

void draw(){
   background(255);
   for(int i=0;i<colors.length;i++){
     fill(colors[i]);
     rect(i*20,0,20,20);
 }
 fill(c);
 rectMode(CENTER);
 rect(width/2,height/2, 100,100);
 danceMat();
}

void danceMat(){

 c=colors[index];
 if(keyPressed){
   if(key==CODED){
     if(keyCode==RIGHT){
       if(index<colors.length-1){
         index = index+1;
       }
     }else if(keyCode==LEFT){
       if(index>0){
         index -= 1;
       }
     }
   }
   print(index+"\n");
 }
}
Re: a probably stupid question about keyPressed
Reply #1 - May 10th, 2010, 11:42pm
 
AHA!

ok instead of putting the boolean keyPressed in a different function I now used the keyPressed() function, and that works.

don't understand the logic behind it, but t works, would like to know the logic though
Re: a probably stupid question about keyPressed
Reply #2 - May 11th, 2010, 2:16am
 
Page Index Toggle Pages: 1