Stop escape key from closing app in new window (G4P)

edited October 2013 in Library Questions


I'm having trouble when using G4P with multiple windows. In my program I have the following code to prevent the escape key from closing the application. This works fine as long as I do not open a new window.

void keyPressed(){ switch(key){ case ESC: key = 0; break; } }

However, when opening a new window, pressing the escape key now closes the application.

The code for the window opener is something like new_win = new GWindow(this, "title", x, y, w, h, false, JAVA2D); new_win.setActionOnClose(GWindow.CLOSE_WINDOW);

If I change the action on close to KEEP_OPEN the escape key still closes the application but the window can't be closed as intended with the close button.

I guess that opening a new window makes keyboard presses to another call function within the new window/applet or something? Is there a way to prevent the escape key from closing the application when a new window has been opened? (Using MS Windows enviroment)

/ Blom



  • Answer ✓

    Effectively each GWindow is a separate Processing sketch with its own event handling mechanism. The setActionOnClose only controls what happens when the user clicks on the window close icon.

    The trick would be to add a key-event-handler for each GWindow and then add your own key handling code.

    This code worked for me.

    import g4p_controls.*;
    GWindow window1;
    public void setup() {
      size(480, 320, JAVA2D);
      window1 = new GWindow(this, "Window title", 0, 0, 240, 120, false, JAVA2D);
      window1.addDrawHandler(this, "win_draw1");
      window1.addKeyHandler(this, "myKeyPress");
    public void draw() {
    // Use this method to add additional statements
    // to customise the GUI controls
    public void customGUI() {
    public void myKeyPress(GWinApplet appc, GWinData data, KeyEvent kevent) {
      switch(appc.key) { 
      case ESC: 
        appc.key = 0; 
    public void keyPressed() { 
      switch(key) { 
      case ESC: 
        key = 0; 
  • Thank you quark. It works like a charm!

Sign In or Register to comment.