Keypressed will put the key currently pressed into the variable "key". You can also keep track of the keyReleased().
void setup(){
size (200, 200);
}
void draw(){
smooth();
if (keyPressed) {
if (key == 'g') || (key == 'G'){
background (255, 20, 20);
}
if (key == 'h') || (key == 'H'){
background (255, 255, 20);
}
}
else{
background(20);
}
}
You can also measure when a key has been released, however we do this in it's own routine
void setup(){
size (200, 200);
}
void draw(){
}
void keyReleased (){
if (key == 'j'){
println("the letter j");
}
}
Of course you can write your own functions, which gets important when you are trying to organize and optimize your code.
void setup(){
size (200, 200);
}
void draw(){
joeisgood();
}
void joeisgood(){
ellipse(width/2, height/2, 20, 20);
}
In this example we are sending three parameters to the function joeisgood, which draws a circle based on the data we send.
void setup(){
size (200, 200);
}
void draw(){
joeisgood(30, 50, 20);
joeisgood(200, 80, 90);
}
void joeisgood(int x, int y, int s){
ellipse(x, y, s, s);
}
Here we use a function to do math on a value and return that value. Notice we don't say "void" before the doMath function!
float val = 30.0;
void draw() {
float t = doMath(val);
println(t);
}
float doMath(float newVal) {
newVal = newVal * 3.14;
return newVal;
}