How to override escape key closing the game

<void keyPressed() {
if (key == ESC) {
key = 0;
}
}>

The only code I’ve found to do it is from 2018 and doesn’t seem to be working in my current application, I still use Processing 4. Are there any other ways to kind of ‘change’ they key that’s pressed?

You need to override the exit method like this

void setup(){
  size(480,320);
}

void draw(){
  background(200,220,200);
}

void exit(){
  // do nothing
}
2 Likes

Wait, so i literally put nothing in the void exit section?

I never knew you could use that as a section in the first place lol.

The exit() function (empty function definition) overrides the default exit() function.

Some insights here to see what is happening:

I just gave you a starting point… you will have to follow the trail of breadcrumbs.

:)

An alternative to keeping the window open when the esc key is pressed is to allow the window to close but perform some cleanup code prior to closing like this

void exit(){
  // perform some clean up here
  // e.g. close open files or whatever
  super.exit(); // now close the window
}
1 Like

Thanks so much for all the info!

1 Like