I came up with a solution for a problem I’ve encountered regarding inputs.
The problem:
When you hold down a key, at least on my computer it registers as if you were repeatedly pressing that key very fast. In some cases you only want to get one single registered keypress, even if the key is held down.
Just to clarify my point with a stupid example: say you were creating a game, and the goal of the game is to press a single key repeatedly, as fast as possible:
int totalPresses = 0;
void setup(){}
void draw(){}
void keyPressed(){
if (key == 'a') {
totalPresses += 1;
}
}
Being able to hold down ‘a’ would ruin the challenge and the game.
The solution:
boolean okay = true; //Create a boolean
totalPresses = 0;
void setup(){}
void draw(){}
void keyPressed(){
if (okay && key == 'a') { //Check if boolean is true
totalPresses += 1; //Do whatever (jump, move, call functions etc...)
okay = false; //Set boolean to false
}
}
void keyReleased() {
okay = true; //Only when the key is released the boolean is set to true again
}
That’s pretty much it.
My reason for posting this is I couldn’t find any solution anywhere no matter where I searched. I’m hoping this will prevent other newbies from that misery