You can certainly build on concepts introduced along with your switch structures:
keyPressed() and keyReleased()
a variable to track if shift key is pressed or not
two separate switch structures; one for key and the other for keyCode
You do not have to check if (key == CODED). Experiment with this.
Focus on readable code and then optimize.
Good topic and it engaged me.
I used a switch/case structure and came out of it with a working and readable example and learned a lot.
boolean shiftPressed = false;
void setup()
{
background(255, 0, 0);
}
void draw()
{
}
void keyPressed()
{
//println(key, keyCode);
switch(key)
{
case 'a':
case 'A':
println("a or A:", key);
break;
// Add extra key cases here
}
switch(keyCode)
{
case SHIFT:
shiftPressed = true;
println("shift pressed");
background(0, 255, 0); // or in draw()
break;
case UP:
if (shiftPressed)
{
println("shift + up");
background(0, 128, 255); // or in draw()
}
if (!shiftPressed)
println("up");
break;
case LEFT:
println("left");
break;
// Add extra keycode cases here
}
}
void keyReleased()
{
if(keyCode == SHIFT)
{
shiftPressed = false;
background(255, 0, 0); // or in draw()
println("shift released");
}
}
A search for “shift” in the forum came up with this which is similar to what I did:
The triple nested if statements work and demonstrate concepts but not *readable:
*Readable is subjective. My group of young programmers could understand the case statements but the nested if statements were a challenge and left for another day.
I focus on readable and maintainable code in my projects these days. Optimizing code and making it lean is in my blood from my embedded programming days but not always necessary and learning to break that habit when it is not necessary. It can be fun though and engages the lobes!