Let LV_KEY_ESC trigger every sub menu back button

Description

Input method: Keypad with ESC, PREV, NEXT, ENTER

How can I configure the menu widget to trigger the “<” back button in a sub page with the ESC key from keypad instead of manually navigating to it with PREV/NEXT and pressing ENTER?

What LVGL version are you using?

9.2

When creating a Menu, the internal button “<” subscribes to the event with the following function

lv_obj_add_event_cb(main_header_back_btn, lv_menu_back_event_cb, LV_EVENT_CLICKED, menu);

Your code should be e.g. (Pseudo):

  1. Page/SubPage pretplatite na event LV_EVENT_KEY

    • lv_obj_add_event_cb(ui_subMenu, subMenu_EscKey_Handler, LV_EVENT_KEY, NULL);
  2. Inside the event function subMenu_EscKey_Handler(lv_event_t * e), check which object called the event and with what data, and then simulate the LV_EVENT_CLICKED event on the “<” object

    • lv_event_send(lv_menu_get_main_header_back_btn(ui_MenuScreen), LV_EVENT_CLICKED, ui_MenuScreen);
  3. Inside the function you need to check which object called the function, whether the object on which you want to invoke the event exists, i.e. whether it is not NULL. etc

Pseudo code that does not contain any additional checks, but should:

static void cont_sub_event_cb(lv_event_t * e)
{
    uint32_t k = lv_event_get_key(e);
    lv_obj_t *obj = lv_event_get_current_target(e);
	
    if(k == LV_KEY_ESC) 
	{
       lv_event_send(lv_menu_get_main_header_back_btn(ui_MenuScreen), LV_EVENT_CLICKED, ui_MenuScreen);
    }
}
1 Like

Thank you. You brought me on the right path:

  1. Put the menu object in the input group
    lv_group_add_obj(inGroup, menu);
  2. Add the event to the menu object (not the page or sub page object!):
    lv_obj_add_event_cb(menu, menu_esc_handler, LV_EVENT_KEY, NULL);
  3. Make sure every entry in the menu bubbles the ESC event up:
    lv_obj_add_flag(cont, LV_OBJ_FLAG_EVENT_BUBBLE);
  4. Trigger the back button programmatically in the event handler of the menu object:
static void menu_esc_handler(lv_event_t * e) {
  if(lv_event_get_key(e) == LV_KEY_ESC) {
    lv_obj_send_event(lv_menu_get_main_header_back_button(menu), LV_EVENT_CLICKED, menu);
  }
}