Disable ESC from exiting program in fullscreen?

I’m trying to use fullscreen and also bind escape to a button. Relevant code below (not complete obviously)

void setup() {
    fullScreen(P2D);
...
}
1 Like

Hi @jonnyetiz

Not clear what you mean !?
But mayby sth. like this. Ignores ESC button but exit on SPACE button…
If you hit ESC the text only changes from A to B, but the sketch will not be closed.
If you hit SPACE the sketch will be closed.

Hope that helps …

String txt = "A";
void setup() {
	fullScreen(P2D);
}

void keyPressed() {
  if (key == ESC) {
    txt="B";
    key=0;    
  }
  else if (key == ' ')
    exit();
}

void draw() {
  background(0,0,255);
  textSize(100);
  textAlign(CENTER,CENTER);
  text(txt,width/2,height/2);
}

Cheers!

1 Like

I actually figured this out not long before your response, but I appreciate the help nonetheless!

@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.

2 Likes