# G4P: how to prevent ESC-Key

**URL:** <https://discourse.processing.org/t/g4p-how-to-prevent-esc-key/3240>\
**Category:** Libraries\
**Created:** [September 4, 2018, 6:04am UTC](https://discourse.processing.org/t/g4p-how-to-prevent-esc-key/3240 "2018-09-04T06:04:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [September 4, 2018, 6:04am UTC](https://discourse.processing.org/t/g4p-how-to-prevent-esc-key/3240/1 "2018-09-04T06:04:34Z")

</div>

Hi,  
I recently discovered that you can stop the ESC-key from closing your program with this code:

```auto
void keyPressed() {
  if (key == ESC) {
    key = 0;
  }
}

```

However when I open a second window with G4P this only works in the main window, since **keyPressed()** is only called when a key is pressed and the main window is currently selscted.

What I want to do is prevent the user from accidentaly exiting the program by pressing the ESC-key in the second window. Is there a way?

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [September 4, 2018, 6:30am UTC](https://discourse.processing.org/t/g4p-how-to-prevent-esc-key/3240/2 "2018-09-04T06:30:54Z")

</div>

Same question for the **dispose()** method, which seems not to be called.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [September 4, 2018, 8:17am UTC](https://discourse.processing.org/t/g4p-how-to-prevent-esc-key/3240/3 "2018-09-04T08:17:34Z")

</div>

you can add keyhandler: like in example  
G4P\_WindowsStarter

```auto
window[i].addKeyHandler(this, "windowKey");

```

for the main window you used like

```auto
void keyPressed() {
  if (key == ESC) {
    key = 0;
    println("main window ESC key / DO NOTHING");
  }
}

```

for the key press ( into active subwindow )

```auto
public void windowKey(PApplet appc, GWinData data, KeyEvent event) {
 // MyWinData data3 = (MyWinData)data;
 if ( event.getAction() == KeyEvent.PRESS ) {
   println("a key pressed");
   if (appc.keyCode == ESC) {
     println("sub window ESC key / DO NOTHING ");
     appc.key = 0; // but better ask if want close window?
  }  
}
}

```

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [September 10, 2018, 12:42pm UTC](https://discourse.processing.org/t/g4p-how-to-prevent-esc-key/3240/4 "2018-09-10T12:42:59Z")

</div>

Hi,  
sorry it took a while to implement this, but now I can report: It works great.

Thank you.

* * *

For those who are curious, dispose(); works like this:

1. Set the “Action on Close” in the G4P Tool to “Close Window” to call the handler when the window is closed when [x] is pressed.

2. Link a method that will be called in the case of closing the window:

```auto
setup_window.addOnCloseHandler(this, "setup_windowClose"); // my window is called setup_window

```

1. This method will then be called:

```auto
public void setup_windowClose(GWindow gwin) { // needs a gwindow which represents the closed window
  exit(); // I use exit here to also close the main window when the secondary is closed
  println("closed secondary");
}

```

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [September 28, 2022, 12:23am UTC](https://discourse.processing.org/t/g4p-how-to-prevent-esc-key/3240/5 "2022-09-28T00:23:55Z")

</div>

@kll

Thank you

Thanks, it worked out well.
