Many beginners and some intermediate coders have run into conflicting issues with using multiple key inputs for their sketches. keyCode and key are single-value variables that track the last key pressed. As such, they cannot store multiple sets of key data on their own.
Thus, I provide the following template:
/*number-indexed keys:*/
boolean[] keys;
void setup() {
/*I use 1 key but you can modify length to include up to all 128 byte values*/
keys = new boolean[1];
for (int i = 0; i < keys.length; i++) {
keys[i] = false;
}
}
void draw() {
if (keys[0]) {
//statements
}
}
void keyPressed() {
if (key == CODED) {
/*keyCode 37 used for demonstration*/
if (keyCode = 37) {
keys[0] = true;
}
}
}
void keyReleased() {
if (key == CODED) {
/*keyCode 37 used for demonstration*/
if (keyCode = 37) {
keys[0] = false;
}
}
}