# How to override escape key closing the game

**URL:** <https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123>\
**Category:** Coding Questions\
**Created:** [September 11, 2025, 4:47pm UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123 "2025-09-11T16:47:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)\
**Post date:** [September 11, 2025, 4:47pm UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123/1 "2025-09-11T16:47:12Z")

</div>

\<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?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 11, 2025, 4:53pm UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123/2 "2025-09-11T16:53:50Z")

</div>

You need to override the `exit` method like this

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

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

void exit(){
  // do nothing
}

```

---

<div class="post-metadata">

**Author:** ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)\
**Post date:** [September 11, 2025, 4:57pm UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123/3 "2025-09-11T16:57:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 11, 2025, 5:24pm UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123/4 "2025-09-11T17:24:26Z")

</div>

> [@WillyGrz](#):
>
> 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:

> <https://github.com/processing/processing4/blob/main/core/src/processing/core/PApplet.java#L2661>

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

`:)`

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 11, 2025, 5:27pm UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123/5 "2025-09-11T17:27:55Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)\
**Post date:** [September 11, 2025, 6:03pm UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123/6 "2025-09-11T18:03:17Z")

</div>

Thanks so much for all the info!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 14, 2025, 6:02pm UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123/7 "2025-09-14T18:02:42Z")

</div>

> [@WillyGrz](#):
>
> if (key == ESC) {  
> key = 0;

it works for me. I use it all the time.

But [quark](https://discourse.processing.org/u/quark)’s method is indeed much better.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 15, 2025, 12:56am UTC](https://discourse.processing.org/t/how-to-override-escape-key-closing-the-game/47123/8 "2025-09-15T00:56:17Z")

</div>

> [@WillyGrz](#):
>
> 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.

It works with Processing 4.4.7 and W10:

```auto
void setup()
  {
  }
  
void draw()
  {
  }
  
void keyPressed()
  {
  if (key == ESC) 
    {
    println(hex(key));  
    key = 0;  
    }
  println(hex(key));  
  }
  
//void exit()
// {
// }

```

`:)`
