FLTK logo

Re: [fltk.general] Fl_Table callback() vs handle() or: Let uncatched key strokes bubble to the default handle() method

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 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Fl_Table callback() vs handle() or: Let uncatched key strokes bubble to the default handle() method Greg Ercolano Nov 02, 2018  
 
On 10/30/18 11:03, hans.templer via fltk.general wrote:
> int My_Fl_Table::handle(int event) {
>     return(Fl_Table::handle(event));
> }
> void My_Fl_Table::event_callback2() {
>     TableContext context = callback_context();
>     switch ( context ) {
>         case CONTEXT_CELL:
>             event = Fl::event();
>             switch (event) {
>                 case FL_KEYBOARD:
>                     if( Fl::event_command() && Fl::event() == 'w' ) {
>                         app.closeWindow();
>                     }
>                 break;
>             }
>         break;
>     }
> }

    A few things, and this in general regarding not just Fl_Table, but all FLTK
    widgets:

		1) There's a bug in your check for the 'w' key; this change is needed:

BEFORE: if( Fl::event_command() && Fl::event() == 'w' ) {
 AFTER: if( Fl::event_command() && Fl::event_key() == 'w' ) {

		   Fl:event_key() is used to get the *keyboard key* for any of the keyboard
		   related events (such as FL_KEYBOARD, FL_SHORTCUT, etc):
		   http://www.fltk.org/doc-1.4/group__fl__events.html#ga1ac131e3cd5ca674cc022b1f77233449

		   Fl::event() returns *event numbers*:
   		   http://www.fltk.org/doc-1.4/group__fl__events.html#gac7595e274aaebaa23982125a1363d13f

		   Note that event numbers returned by Fl::event() (or passed as the integer argument
		   to all handle() methods) are defined as Fl_Event enums, e.g.:

			    FL_KEYBOARD, FL_PUSH, FL_RELEASE, FL_SHORTCUT..

		   ..the complete list of those is here:
		   http://www.fltk.org/doc-1.4/Enumerations_8H.html#ad16daf120d9a0501cccaee563af0b9a3

		2) For trapping something like a close-window keyboard key (Command-W),
		   this is more of an FL_SHORTCUT type of event rather than FL_KEYBOARD.

		   And you might want to handle it at the Fl_Window level instead of
		   the table level, but that's up to you.

		   Or perhaps best: don't use widget handle() methods at all in this case
		   to look for a global shortcut; use Fl::add_handler() instead:
		   http://www.fltk.org/doc-1.4/group__fl__events.html#gae2d39bda7362e444afa41166c478b904
		   ..this lets you look for the shortcut key regardless of which windows
		   are open, even unrelated dialog windows.

		3) If you decide to stick with handling the event inside widgets,
		   it's best to do all keyboard/mouse event handling inside the handle()
		   method, not the callback.

		   The handle() method is called whenever any event is sent to the widget.
		   All event handling is filtered through this method.

		   Not the case for the widget's callback(); the widget's callback()
		   is invoked conditionally, and the behavior is very widget-specific.

		   So I would suggest removing all of this:

----
> void My_Fl_Table::event_callback2() {
>     TableContext context = callback_context();
>     switch ( context ) {
>         case CONTEXT_CELL:
>             event = Fl::event();
>             switch (event) {
>                 case FL_KEYBOARD:
>                     if( Fl::event_command() && Fl::event() == 'w' ) {
>                         app.closeWindow();
>                     }
>                 break;
>             }
>         break;
>     }
> }
----

		   ..and replacing this:

----
> int My_Fl_Table::handle(int event) {
>     return(Fl_Table::handle(event));
> }
----

		   ..with this:

----
  int My_Fl_Table::handle(int event) {
    switch ( event ) {
      case FL_SHORTCUT:        // Look for keyboard 'shortcuts'
        if ( Fl::event_command() && Fl::event_key() == 'w' ) {
          app.closeWindow();
          return(1);           // <-- important: tells FLTK event 'handled' so it doesn't propagate further
        }
        break;
    }
    return(Fl_Table::handle(event));
  }
----

    ..if you want your table widget to handle this event. (See caveats in (2)).

    I've attached a modified version of the table-simple.cxx example program here
    that opens three windows each with an Fl_Table, and uses the handle() technique
    described in (3) to demonstrate how it works. So in any of the 3 windows opened,
    Command-W should close that window. It could also close the application if you want;
    just replace the hide() with e.g. exit(0).

-- 
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

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'.