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 › Counters or KeyListeners
Page Index Toggle Pages: 1
Counters or KeyListeners (Read 388 times)
Counters or KeyListeners
Feb 4th, 2008, 6:11pm
 
Hello,

I'm not entirely new to programming, but I am new to Processing. Through my research of the language, I've found that there is no Counter or KeyListeners. Is this the case?

What I'm trying to do is change the background color based on how many time the users presses the UP or DOWN keys (this can be comparable to F1 and F2 keys changing the screen brightness on the Mac).

Anyone have any helpful nuggets of information?

~N. Kaz
Re: Counters or KeyListeners
Reply #1 - Feb 4th, 2008, 7:22pm
 
the reference is always a good point to start Tongue
http://processing.org/reference/keyCode.html
Re: Counters or KeyListeners
Reply #2 - Feb 5th, 2008, 2:26am
 
I got that far.

I successfully wrote a program that changed the color of the background to white (when UP is pressed) and to black (when DOWN is pressed) using keyCode. My issue was I couldn't seem to get it to count the different times that I hit UP/DOWN (if keypressed UP == true then x = x + 1) and then apply that to changing a background.
Re: Counters or KeyListeners
Reply #3 - Feb 5th, 2008, 2:45am
 
You can store a current value for the background in a variable, then modify that variable whenever a key is pressed.

Code:

int bgColor = 0;

void keyPressed(){
bgColor += 1;
background( bgColor );
}


Alternatively, you could store the colors you want in an array, and use the variable as the key for the array.

Code:

int[] colors = new int[10];
int index = 0;

void setup(){
size(100,100);

for( int i = 0; i < colors.length; i++ ){
colors[i] = color( random(255), random(255), random(255) );
}

}

void draw(){
//initiate listeners for key presses
}

void keyPressed(){
println("key pressed" );
if( keyCode == UP ){
index++;
println("up was pressed");
} else if ( keyCode == DOWN ){
index--;
}

if( index < 0 ) index = colors.length - 1;
if( index >= colors.length ) index = 0;

background( colors[index] );
}


Page Index Toggle Pages: 1