The following code works exactly as it should, but I want to understand the event flow. The change to varH should happen when the key is pressed and only happen once.
Under void keyPressed()
When the H or h key is pressed I have
changeH = true;
BUT, and here is my question, why is
changeH = true;
not needed for when the G or g key is pressed?? Everything works fine whether I include the switch for the G\g key or not. Can someone please explain this to me?
boolean changeH = true;
int varH = 204;
void setup() {
size(800, 400, P2D);
noStroke();
colorMode(HSB, 360, 360, 360);
}
void draw() {
fill(204, 360, 360);
rect(0, 0, 400, 400);
fill(varH, 360, 360);
rect(400, 0, 400, 400);
}
void keyPressed() {
if ((key == 'H') || (key == 'h')) {
changeH = true; // why only here?
varH = varH + 1;
} else if ((key == 'G') || (key == 'g')) {
//changeH = true; // why not here?
varH = varH - 1;
}
println(varH+" "+changeH);
}
void keyReleased() {
changeH = false;
}