Programmatically change the focus, or defocus an object (not in a group)?


Is there a way to programmatically change the focus, or defocus an object (not in a group)?

(exactly what I am trying to do, and a short video, below

Environment: What MCU/Processor/Board and compiler are you using?

Visual Studio Code/ Platformio/espressif32/arduino framework
LVGL 8.3.6
Squareline Studio current release

What do you want to achieve?

I have a nice UI built with a textArea and a hidden keyboard. The keyboard is unhidden when the user clicks on the textArea. I am hiding the keyboard and moving the textArea after the done (checkmark) key is clicked on the keyboard. So far, so good. Now if the user wants to re-enter the textArea, just clicking on the textArea does nothing. The user first has to click outside the textArea and then click in the textArea in order to bring the focus back to the textArea so they can change the text.

Since I have a function which processes the input once the user presses the done (checkmark) key, is there an lgvl command I can use to put the focus somewhere else (like just on the screen) so if the user wants to click on the textArea the focus will go to the textArea and my events will be triggered (without the user having to first click outside the textArea)?

This seems like a basic thing to do, and surely I am missing a fundamental principle here, so THANK YOU for your consideration in helping out here.*

What have you tried so far?

Searched until I could search no more. (There is a restaurant at the end of the internet… also)

 `lv_obj_clear_state(ui_TextAreaEmailAddress, LV_STATE_FOCUSED);`


`lv_obj_add_state(ui_LabelTestTimeValue, LV_STATE_FOCUSED);`

Screenshot and/or video

this short video shows the problem

How you do this ?

@Marian_M There is an excellent video on this here:
I highly recommend this video and the creator’s series on LVGL using SLS.
The video does stop short of a few things like clearing the textArea when done so that the text does not appear the next time the textArea is opened. For that I add something like:
lv_textarea_set_text(ui_TextAreaEmailAddress, "");
to the code where the screen is initialized and/or where the focus on the textArea is responded to in the code.

But still, I don’t have an answer for how to programatically remove the focus from the textArea so one can use it again without having to first click somewhere else on the screen.

Video miss situation for one textarea or other … Try

@Marian_M That is not really addressing my problem. It is not keyboard that is at issue. It is not being able to bring the focus back to the textArea AFTER the keyboard has a READY state from the checkmark click. (Did you see that in the video)?

Why not i place event in keyboard to hide on ready and too defucus textarea, but this only hide cursor. And on textarea to hide on defocused. But for unhide not used focused because this dont work as you write .
For me this work ok in simulator. But cursor is not show on second show keyboard. You ask as howto then this work. And yoes is buggy . My tip on second click textarea require back add focus simple next event change state.

The simplest solution for your problem is probably setting the unhiding of the keyboard for the textarea’s ‘CLICKED’ event too, not just the ‘FOCUSED’ event. I created a project where the focused state is shown by a thicker border on the textarea, and it doesn’t seem to lose the focus because of the READY signal from the keyboard and hiding it. Your problem is that it’s left as focused and no new trigger happens because of it. But if you want you can set/remove the FOCUSED state from code for your object programmatically by lv_obj_add_state(obj, LV_STATE_FOCUSED) and lv_obj_clear_state(obj, LV_STATE_FOCUSED) as LVGL documentation describes.

1 Like

@Hermit, Thank you. I find that there is no LV_OBJ_FLAG_FOCUSED but only a LV_STATE_FOCUSED and I had already tried
lv_obj_clear_state(ui_TextAreaEmailAddress, LV_STATE_FOCUSED);
without accomplishing the objective.

I did though sort of follow your advice regarding using CLICKED instead of FOCUSED but instead of having both triggers, I just changed CLICKED to FOCUSED and so far that seems to have fixed the problem.