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 › Async Key Request
Page Index Toggle Pages: 1
Async Key Request (Read 1091 times)
Async Key Request
Oct 12th, 2009, 12:25pm
 
Hi,

I'm sorry for opening a thread about this topic, maybe there's already one in the forum. I don't know.

I just loaded a picture of a small spaceship and I can control its position by using 'a' and 'd'.
Then I said that when pressing 'k' the ship must fire. It's doing very well, but unfortunately I can't press two keys at the same time.

That means, I can just fire when the ship doesn't move.
Is there any possibility to achieve a proper navigation for such a simple spaceship?

Hope you understand my intention.
Sorry for my bad englisch, I'm from Germany Wink
Re: Async Key Request
Reply #1 - Oct 12th, 2009, 1:09pm
 
Multiple keypresses can be handled by flagging booleans 'true' when a key is pressed and 'false' when the same key is released...you need a boolean for each key.  e.g.:

boolean up, down, left, right;

void keyPressed(){
 switch(key){
   case('a'):
   left = true;
   break;
   case('d'):
   right = true;
   break;
 }
}

void keyReleased(){
 switch(key){
   case('a'):
   left = false;
   break;
   case('d'):
   right = false;
   break;
 }
}

void draw(){
 checkKeys();
}

void checkKeys(){
 if (left)... // move left
 if (right)... // move right
}
Re: Async Key Request
Reply #2 - Oct 12th, 2009, 1:35pm
 
Hi,
it's that easy? Amazing Smiley

Thanks a lot, this really helps me!
Re: Async Key Request
Reply #3 - Oct 12th, 2009, 2:21pm
 
It is a topic that has come up before.  If you did try to search, watch out for the 'This post was made in the last...' option, as it defaults to a week; which isn't nearly long enough...
Re: Async Key Request
Reply #4 - Oct 12th, 2009, 3:09pm
 
Yeah, if I'd had known what to look for I would have found something. Actually I tried the search function with the topic keywords and it always said: "sorry, 0 hits"  Roll Eyes

I didn't know how to correctly name the problem, you know?
And first of all I explained I'm sorry for this. So don't shout at me.  Wink
Page Index Toggle Pages: 1