@jonnyetiz – there are at several ways to approach this.
One is to replace the key value, as you did above, so that a later check does not trigger the sketch exit()
method.
Another way is to override the sketch exit()
method and make it dependent, not on key, but on some gatekeeper value of your own such as boolean doExit
.
boolean doExit=false;
void draw() {
}
void keyPressed() {
if(key==' ') {
doExit=true;
exit();
}
}
void exit(){
if (doExit==true){
super.exit();
}
}
Now exit()
does nothing unless your flag is set, in which case it exits normally. That means you can trigger exiting in as many ways as you want – with a keypress, or a mouse click, or a timer, or all of the above – and you aren’t limited to only a different keyboard key.
The only thing to be aware of about this design is that now the sketch applet won’t actually stop anymore if you close the applet window with the X / red button without running code that sets your boolean flag – it will still appear running in your toolbar, and you will need to use the PDE “stop” square button to quit it.
If you wish to handle that case as well, you may use an exit hook or dispose: Save when app close, exit() ... stop()
Or just stick to the keyPresses only solution.