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.
Page Index Toggle Pages: 1
keypress (Read 456 times)
keypress
Apr 21st, 2006, 9:19pm
 
Hi,

I am a beginner, and i looked at the exampe files of key and keyPressed, and they are not helpful to me, because they don't show an example with a specific key that is pressed in order to activate something. This is what I want to do:

I want to make things happen inside an if sentence when I press 1
when I press 2 I want another if sentence to be activated

Below is my code. What happens here is, that only when I hold down the key it activates the if sentence:

   // STATE A
   if(keyReleased) { if (key == 'p') {
     count++; // Increment the counter
       //if(count == 1) { aniShp[0] = 3 ; aniShp[1] = 6; aniShp[2] = 12; aniShp[3] = 18; aniShp[4] = 24; }
       if(count >= 30) { count = 0; }
       if(count >= 1) { shp[0]=21; shp[1]=1; shp[2]=8; shp[3]=20; shp[4]=20; shp[5]=0; shp[6]=0; shp[7]=0; shp[8]=0; shp[9]=0; shp[10]=0; shp[11]=0; }
       if(count >= 6) { shp[0]=21; shp[1]=1; shp[2]=8; shp[3]=20; shp[4]=20; shp[5]=0; shp[6]=0; shp[7]=-7; shp[8]=0; shp[9]=-18; shp[10]=0; shp[11]=0; }
       if(count >= 12) { shp[0]=3; shp[1]=1; shp[2]=1; shp[3]=-6; shp[4]=20; shp[5]=0; shp[6]=0; shp[7]=-7; shp[8]=0; shp[9]=18; shp[10]=0; shp[11]=0; }
       if(count >= 18) { shp[0]=0; shp[1]=5; shp[2]=8; shp[3]=-20; shp[4]=20; shp[5]=0; shp[6]=0; shp[7]=-7; shp[8]=0; shp[9]=18; shp[10]=0; shp[11]=0; }
       if(count >= 24) { shp[0]=0; shp[1]=5; shp[2]=8; shp[3]=-20; shp[4]=20; shp[5]=14; shp[6]=0; shp[7]=-7; shp[8]=0; shp[9]=18; shp[10]=0; shp[11]=0; }

       }
   }






Re: keypress
Reply #1 - Apr 21st, 2006, 10:04pm
 
annemarie wrote on Apr 21st, 2006, 9:19pm:
they don't show an example with a specific key that is pressed in order to activate something.


Surely that is the only thing that example shows you how to do. By your description you want in your code something like:
Code:

void setup(){
}
void draw(){
}
void keyReleased(){
if(key == '1'){
println("sentence 1");
}
if(key == '2'){
println("sentence 2");
}
}

Alternatively if you find yourself writing a lot of if statements in a row, you can use switch(), which is like a multi-if statement.
Code:

void setup(){
}
void draw(){
}
void keyReleased(){
switch(key){
case '1':
println("sentence 1");
break;
case '2':
case '3':
println("sentence 2");
break;
default:
println("sentence 3");
break;
}
}

You can't actually do if(keyReleased) because it's an event listener that you use a bit differently to keyPressed. You write it as a method and when that event happens, the stuff in the event listener block is run.
Page Index Toggle Pages: 1