1

我有一个自定义类,CustomButton它扩展了Fl_Button. 在我的屏幕上有一堆Fl_InputCustomButton部件,我希望能够使用选项卡按键在它们之间导航。输入字段之间CustomButton的标签工作正常,但一旦获得焦点,我似乎无法离开它。

这是我的句柄功能

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 );
}

我正在使用 fltk 1.1.10。

4

2 回答 2

0

有关演示如何控制焦点的非常简单的最小示例,请查看navigation.cxx测试文件。

也许您的小部件确实获得了焦点(使用 Fl::focus() 检查),但它没有显示出来(您需要处理 FL_FOCUS 和/或 FL_UNFOCUS 事件)?

于 2020-05-05T15:52:39.903 回答
0

我的问题是我在没有实际使用 tab 键的情况下CustomButton::handle()返回事件。1FL_KEYBOARD

is_event_handled = 1进入 if 语句让我FL_Enter只使用按键并让其他小部件(即控制导航的 Fl_Group)接受任何其他按键。

或者摆脱if并用类似的东西替换

switch(Fl::event_key())
{
    case FL_Enter:
    case FL_KP_Enter:
        // Do stuff
        is_event_handled = 1;
        break;
    default:
        is_event_handled = 0;
        break;
}
于 2020-05-06T14:46:22.737 回答