Hide keyboard on pressing "enter"

I wanted to ask if anyone knows how I could get the keyboard to disappear and have the user leave focus from the text area after pressing the return/enter key on the keyboard.

Do you mean on the keyboard widget or the keyboard of your PC?

If you are talking about LVGL keyboard, then you can consider using the ā€œFOCUSEDā€ event of the keyboardā€™s textarea.

  • If the textarea is focused, add the ā€œfocusedā€ event to unhide the keyboard
  • If you press enter of the keyboard, the textarea will become ā€œunfocusedā€, then you can add a event to hide the keyboard.

I am referring to the keyboard widget. I can understand how to achieve my intended functionality by working with the ā€œFOCUSEDā€ event, but when I press enter on the keyboard, it just creates a new line rather than defocusing from the text area. If I set the text area to ā€œOne line modeā€ then pressing enter does not do anything.

The keyboard widget. If I can get the keyboard to defocus after pressing enter, I think I can easily achieve my desired outcome, but I am currently struggling to get that to work.

You should press the ā€œCHECKā€ symbol (āˆš) of the keyboard to finish input which will also ā€œdefocusā€ the text input widget.

Pressing that key also does nothing.

Are you sure?

keyboard

To hide the keyboard when pressing its Enter key, you can add a VALUE_CHANGE event to the keyboard, and use a ā€œCall functionā€ action. In that custom function you can do something like:

  lv_obj_t * kb = lv_event_get_target(e);
  uint32_t btn_id = lv_keyboard_get_selected_btn(kb);
  const char * txt = lv_keyboard_get_btn_text(kb, btn_id);

  if(strcmp(txt, LV_SYMBOL_NEW_LINE) == 0) {
      lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
  }
1 Like

Thanks for this. Iā€™ve just realised the elegance of this method (I didnā€™t know about lv_event_get_target). It means itā€™ll work for any keyboard against which is is called. Thanks!