# How to disable ESC key functionality in G4P child window

**URL:** <https://discourse.processing.org/t/how-to-disable-esc-key-functionality-in-g4p-child-window/19891>\
**Category:** Libraries\
**Created:** [April 18, 2020, 2:09pm UTC](https://discourse.processing.org/t/how-to-disable-esc-key-functionality-in-g4p-child-window/19891 "2020-04-18T14:09:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/david/32/8853_2.png) [@David](https://discourse.processing.org/u/David)\
**Post date:** [April 18, 2020, 2:09pm UTC](https://discourse.processing.org/t/how-to-disable-esc-key-functionality-in-g4p-child-window/19891/1 "2020-04-18T14:09:32Z")

</div>

Problem: I want to disable the default functionality of the ESC key in a Processing sketch that creates a G4P child window. The standard method of doing this is:

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

```

But this only works in the main window, not the child window. So I created an event handler for the child window and registered it. It works in the sense that it captures key presses in the child window, but pressing the ESC key in the child window still closes both windows. How can I prevent this from happening? Here’s the event handler:

```auto
public void keyCheck(PApplet applet, GWinData data, KeyEvent event) {
  if (event.getAction() == event.PRESS) {
    keyCode = event.getKeyCode();
    println("keyCode = " + keyCode);
    if (event.getKeyCode() == ESC) {
      // Now do what?
    }
  }
}

```

Thanks.

---

<div class="post-metadata">

**Author:** ![David](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/david/32/8853_2.png) [@David](https://discourse.processing.org/u/David)\
**Post date:** [April 18, 2020, 2:24pm UTC](https://discourse.processing.org/t/how-to-disable-esc-key-functionality-in-g4p-child-window/19891/2 "2020-04-18T14:24:39Z")

</div>

Here’s the complete sketch for anyone that wants to try it:

```auto
import g4p_controls.*;

GWindow window;
GButton btnMakeWindow;

void keyPressed() {
  if (key == ESC) {
    println("main window: pressed ESC key");
    key = 0;
  }
}

public void setup() {
  size(300, 240, JAVA2D);
  // Turn off mouse over icon change due to Processing bug
  G4P.setMouseOverEnabled(false);
  createGUI();
}

public void draw() {
  background(230);
}

public void keyCheck(PApplet applet, GWinData data, KeyEvent event) {
  if (event.getAction() == event.PRESS) {
    keyCode = event.getKeyCode();
    println("keyCode = " + keyCode);
    if (event.getKeyCode() == ESC) {
      println("child window: pressed ESC key");
      // Do what?
    }
  }
}

public void makeWindow(GButton source, GEvent event) {
  window = GWindow.getWindow(this,"Child Window",200,400,600,300,JAVA2D);
  window.addKeyHandler(this,"keyCheck");
} 

public void createGUI(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setCursor(ARROW);
  surface.setTitle("Main Window");
  btnMakeWindow = new GButton(this, 10, 190, 250, 30);
  btnMakeWindow.setText("Make the window");
  btnMakeWindow.setTextBold();
  btnMakeWindow.addEventHandler(this, "makeWindow");
}

```

---

<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:** [April 18, 2020, 5:16pm UTC](https://discourse.processing.org/t/how-to-disable-esc-key-functionality-in-g4p-child-window/19891/3 "2020-04-18T17:16:42Z")

</div>

Each window is a separate PApplet so you must use the `key` variable for the correct applet - this worked for me

```auto
public void keyCheck(PApplet applet, GWinData data, KeyEvent event) {
  if (event.getAction() == event.PRESS) {
    applet.keyCode = event.getKeyCode();
    println("keyCode = " + keyCode);
    if (event.getKeyCode() == ESC) {
      println("child window: pressed ESC key");
      applet.key= 0;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![David](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/david/32/8853_2.png) [@David](https://discourse.processing.org/u/David)\
**Post date:** [April 18, 2020, 5:27pm UTC](https://discourse.processing.org/t/how-to-disable-esc-key-functionality-in-g4p-child-window/19891/4 "2020-04-18T17:27:22Z")

</div>

Works for me too, except for the first “println()”. I had to add the “applet.” qualifier to it as well, otherwise it prints the last key pressed in the main window.

` println("keyCode = " + applet.keyCode);`

Thanks!
