There doesn't seem to be an easy and straightforward way to close a form when Esc is pressed (without resorting to subclassing the window, or registering an event handler for every child widget). form::keyboard_accelerator doesn't work with the escape key.

There doesn't seem to be an easy and straightforward way to close a form when Esc is pressed (without resorting to subclassing the window, or registering an event handler for every child widget). `form::keyboard_accelerator` doesn't work with the escape key.

Use register_shortkey.

form fm;

   fm.register_shortkey(keyboard::escape);

   fm.events().key_release([&](const arg_keyboard& arg){
      if (arg.key == keyboard::escape)
         fm.close();
   });

   fm.caption("Main Form");

   button bn(fm, "Test");
   place layout{fm};
   layout.div("vert <><><margin = 16<><weight = 120 Button><>>");
   layout["Button"] << bn;
   layout.collocate();

   fm.show();

    exec();

    return 0;
Use register_shortkey. ```` form fm; fm.register_shortkey(keyboard::escape); fm.events().key_release([&](const arg_keyboard& arg){ if (arg.key == keyboard::escape) fm.close(); }); fm.caption("Main Form"); button bn(fm, "Test"); place layout{fm}; layout.div("vert <><><margin = 16<><weight = 120 Button><>>"); layout["Button"] << bn; layout.collocate(); fm.show(); exec(); return 0; ````

Thanks, but that doesn't work. If you move the input focus away from the form (to a child widget, like the button in your example), the event is eaten by the child widget, and never reaches the form. I'm not even sure what widget::register_shortkey is supposed to do. What's a "shortkey"?

Whatever shortkeys do, they only work for letter keys: https://github.com/cnjinhao/nana/blob/3cb7dfae0245d364bc46851195622727b2c68aeb/source/gui/detail/window_manager.cpp#L75

Thanks, but that doesn't work. If you move the input focus away from the form (to a child widget, like the button in your example), the event is eaten by the child widget, and never reaches the form. I'm not even sure what `widget::register_shortkey` is supposed to do. What's a "shortkey"? Whatever shortkeys do, they only work for letter keys: https://github.com/cnjinhao/nana/blob/3cb7dfae0245d364bc46851195622727b2c68aeb/source/gui/detail/window_manager.cpp#L75
edited Aug 26 at 1:13 am

You're right. shortkey is like an accelerator.... it uses with Alt key.

I guess the only way is to use enum_widgets to register for all the child widgets....

You're right. shortkey is like an accelerator.... it uses with Alt key. I guess the only way is to use enum_widgets to register for all the child widgets....
23
views
3
replies
2
followers
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft