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
Multikey codes (Read 559 times)
Multikey codes
Feb 12th, 2009, 2:53am
 
Hi there. If you want to write a keyPressed function whose behavior is determined by a multi-key entry, such as "Ctrl +", or "Ctrl H", how would you check for a key combo like that?
Re: Multikey codes
Reply #1 - Feb 12th, 2009, 7:28am
 
what about

if(keyPressed) {
   if (key == 'A' && key == 'B') {
     do something;
   }
Re: Multikey codes
Reply #2 - Feb 12th, 2009, 8:21am
 
That doesn't seem to work.
Re: Multikey codes
Reply #3 - Feb 12th, 2009, 8:31am
 
boolean[] keys;
void setup()
{
 keys = new boolean[512]; // just to be sure. i don't remember the highest keyCode value right now Tongue
}
void draw()
{
if(keys[CONTROL] && keys[32]){ // CTRL + SPACE
 println("HELLO WORLD");
}
}

void keyPressed()
{
 println(keyCode); // to find out the different keyCodes for the different chars, read the output from this.
 keys[keyCode] = true;
}
void keyReleased()
{
 keys[keyCode] = false;
}
Re: Multikey codes
Reply #4 - Feb 12th, 2009, 8:33am
 
allright, i never needed it so it was just a guess. but its working this way... maybe clean it up a bit, but it works...


boolean a = false;
boolean s = false;

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

void draw(){

 background(255);
 fill(255);
 if(keyPressed) {
   if (key == 's' ) {
     s = true;
   }
 }
 else{
   s=false;
 }

 if(keyPressed) {
   if (key == 'a' ) {
     a = true;
   }
 }
 else{
   a=false;
 }

 if(a==true && s==true)fill(255,0,0);

 stroke(0);
 ellipse(200,200,200,200);

}


Re: Multikey codes
Reply #5 - Feb 12th, 2009, 8:34am
 
damn to slow Smiley
Re: Multikey codes
Reply #6 - Feb 12th, 2009, 8:40am
 
hmm anyway, guess his way looks better. in my case you have to release both buttons to activate it again. releasing just one button doesnt do it...
Re: Multikey codes
Reply #7 - Feb 12th, 2009, 9:05am
 
Awesome - thank you both!
Re: Multikey codes
Reply #8 - Feb 12th, 2009, 11:53am
 
Page Index Toggle Pages: 1