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 › Keyboard-Input 101
Page Index Toggle Pages: 1
Keyboard-Input 101 (Read 1549 times)
Keyboard-Input 101
Dec 5th, 2009, 4:29am
 
Hi!

Well, I guess there should be a tutorial for what I want to know somewhere out there in the internets but I didn't find any that briefly explain exactly what I want to know:


When using the keyboard as a means of input in Processing, I always come across some difficulties. My questions according to those:

1.: How can I make my program react to input from multiple different keys simultaneously and independently? (so it doesn't ignore one pressed key once I press another etc.)
2.: How can I make the program only react once for every keypress of a certain key until it's released and pressed again? So... if I made a platformer and wanted the character to jump only once whenever a keypress on the "jump"-key is detected opposed to having him jump up and down if the key is held down.

What are the simplest ways of accomplishing those things?
Are there finished standard-functions for keyboard-input already that I could somehow implement into my code? (with which I mean: functions that handle things like multiple simultaneous keypresses and key-tapping)

Thanks in advance,
Lukas
Re: Keyboard-Input 101
Reply #1 - Dec 5th, 2009, 5:17am
 
As you might imagine the first part of this question gets asked a lot - so a search might have been a good idea Wink

I think the last time I answered it with one possible approach was here...

The second part shouldn't be too difficult - just add another boolean.  Here's some pseudo-code:

Code:
// onPress:
 if(space) {
// note that '!jumped' is equivalent to 'jumped == false'
   if(!jumped) { // if jump hasn't already happened this keypress
     jump(); // jump!
     jump = true; // Make sure jump won't happen again until key is released
   }
 }

// onKeyRelease:
if(space) {
 jump = false;  // jump is false so the next key press will allow a jump
}
Page Index Toggle Pages: 1