FLTK logo

Article #378: Why Does the Escape Key Close My Window?

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 Home  |  Articles & FAQs  |  Bugs & Features  |  Documentation  |  Download  |  Screenshots  ]
 

Return to Articles | Show Comments | Submit Comment ]

Article #378: Why Does the Escape Key Close My Window?

Created at 07:59 Jan 29, 2005 by mike

1. FLTK has a "global event handler" that makes Escape try to close the window, the same as clicking the close box.  To disable this everywhere you can install your own that pretends it wants the escape key and thus stops the default one from seeing it (this may not be what you want, see below about the callbacks):

static int my_handler(int event) {
  if (event == FL_SHORTCUT) return 1; // eat all shortcut keys
  return 0;
}
...in main():
  Fl::add_handler(my_handler);
...

2. Attempts to close a window (both clicking the close box or typing Escape) call that window's callback.  The default version of the callback does hide().  To make the window not close or otherwise do something different you replace the callback.  To make the main window exit the program:

void my_callback(Fl_Widget*, void*) {
  exit(0);
}
...
  main_window->callback(my_callback);
...

If you don't want Escape to close the main window and exit you can check for and ignore it.  This is better than replacing the global handler because Escape will still close pop-up windows:

void my_callback(Fl_Widget*, void*) {
  if (Fl::event()==FL_SHORTCUT && Fl::event_key()==FL_Escape) 
    return; // ignore Escape
  exit(0);
}

It is very common to ask for confirmation before exiting, this can be done with:

void my_callback(Fl_Widget*, void*) {
  if (fl_ask("Are you really absolutely positively"
             " certain you want to quit?"))
    exit(0);
}

Download | Home Page | Listing ]


Comments

Submit Comment ]

From willbproggin, 11:13 Apr 06, 2023 (score=3)

This was a life-saver for me today!  Thanks from 2023! :-)
Reply ]

 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.