简体   繁体   中英

How can I tab away from a Fl_Button in FLTK?

I have a custom class, CustomButton that extends Fl_Button . On my screen there are a bunch of Fl_Input and CustomButton widgets that I want to be able to navigate between using a tab keypress. Tabbing between input fields works fine, but once a CustomButton gets focus, I can't seem to tab away from it.

Here's my handle function

int CustomButton::handle ( int event )
{
  int is_event_handled = 0;
  switch (event)
  {
    case FL_KEYBOARD:
      // If the keypress was enter, toggle the button on/off
      if (Fl::event_key() == FL_Enter || Fl::event_key() == FL_KP_Enter)
      {
        // Do stuff...
      }
      is_event_handled = 1;
      break;

    case FL_FOCUS:
    case FL_UNFOCUS:
      // The default Fl_Button handling does not allow Focus/Unfocus
      // for the button so mark the even as handled to skip the Fl_Button processing
      is_event_handled = 1;
      break;

    default:
      is_event_handled = 0;
      break;
  }

  if ( is_event_handled == 1 ) return 1;
  return Fl_Round_Button::handle ( event );
}

I am using fltk 1.1.10.

For a very simple, minimal example which demonstrates how to control the focus, please check the navigation.cxx test file.

Maybe your widget did get the focus (check this with Fl::focus()), but it does not show that up (you need to handle the FL_FOCUS and/or FL_UNFOCUS events)?

My problem was my CustomButton::handle() returning 1 after a FL_KEYBOARD event without actually using the tab key press.

Moving is_event_handled = 1 into the if statement lets me consume the FL_Enter keypress only and lets other widgets (ie the Fl_Group that controls navigation) take any other keypresses.

Alternatively get rid of the if and replace with something like

switch(Fl::event_key())
{
    case FL_Enter:
    case FL_KP_Enter:
        // Do stuff
        is_event_handled = 1;
        break;
    default:
        is_event_handled = 0;
        break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM