Description
When a textarea receives focus, I want to select all of its text. But, when I do what I think should do to select the text, no text is selected – instead, the cursor is simply placed according to where the textarea was clicked (as if my focus event handler wasn’t called, but it is called according to Serial reports).
What MCU/Processor/Board and compiler are you using?
ESP32-2432S028R (cheap yellow display)
What LVGL version are you using?
9.2.2
What do you want to achieve?
All text in a textarea is selected when it receives focus (this prepares to replace all existing text upon the next character input, similar to what happens when editing forms on most desktop GUIs and Android).
What have you tried so far?
I have tried to programmatically select all text of the textarea in the textarea’s focus event handler (see code).
I’m unclear on where and when lv_textarea_set_text_selection should be called (once ever upon load, or every focus/defocus as I have here), but it doesn’t seem to matter for this problem.
Code to reproduce
void ta_Focused(lv_event_t* e) {
Serial.println("ta_Focused");
taSelected = (lv_obj_t*)lv_event_get_user_data(e);
lv_textarea_set_text_selection(taSelected, true);
length_t n = strlen(lv_textarea_get_text(taSelected));
lv_obj_t* ta_label = lv_textarea_get_label(taSelected);
lv_label_set_text_selection_start(ta_label, 0);
lv_label_set_text_selection_end(ta_label, n);
Serial.print("Selected 0 to "); Serial.println(n);
}
void ta_Defocused(lv_event_t* e) {
Serial.println("ta_Defocused");
lv_obj_t* ta = (lv_obj_t*)lv_event_get_user_data(e);
lv_textarea_set_text_selection(ta, false);
}
void scrWheel_Loaded(lv_event_t* e) {
lv_obj_add_event_cb(ui_taCompressed, ta_Focused, LV_EVENT_FOCUSED, ui_taCompressed);
lv_obj_add_event_cb(ui_taCompressed, ta_Defocused, LV_EVENT_DEFOCUSED, ui_taCompressed);
lv_obj_add_event_cb(ui_taExtended, ta_Focused, LV_EVENT_FOCUSED, ui_taExtended);
lv_obj_add_event_cb(ui_taExtended, ta_Defocused, LV_EVENT_DEFOCUSED, ui_taExtended);
}