Save textarea input to string // Delete textarea after clicking cancel/apply

Description

I want to click a button so a keyboard and a textarea is opening up on the active screen. then I want to enter a number on the keyboard displayed on the touchscreen. This working so far.
If I click apply on the keyboard the number in the textarea should be saved to a string. But I don’t know how to even save the textarea input into a string variable.

A second problem is i don’t find anything working command to close/delete the textarea. the keyboard closes/deletes nicely. But the textarea is still beeing displayed.

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

ESP32 Devikit C// Arduino IDE

What do you want to achieve?

I want to save the the input from textarea to a string variable.
Also I want the textarea to be closed when the keyboard gets closed.

What have you tried so far?

I tried to read the textarea like mentioned in the LittlevGL documentation but I need some help how to get on.
I tried to send the content of the pointer from textarea to serial monitor but the values are not right no matter what i try ( %d,%u,%c,%s,%n)
And I am kind of new to programming so please don#t be angry if this are stupid questions.

Code to reproduce

Here is my event for the keyboard event

static void keyboard_event_cb(lv_obj_t * kb, lv_event_t event)
{
    lv_kb_def_event_cb(kb, event);

    if(event == LV_EVENT_APPLY)) 
      { 
        const char *lv_ta_get_text(const lv_obj_t *ta);
       
                                          // (this is location where i want to save the 
                                          // content from textarea  to a string variable later)
        
        printf("%d\n", *lv_ta_get_text); // it tried %d,%u,%c,%n and so on, but the content from 
                                             //textarea is not displayed properly in serial monitor
        lv_obj_del(kb);
        kb = NULL;
        
        

     }
    
    if(event == LV_EVENT_CANCEL)
    {
      lv_obj_del(kb);
      kb = NULL;
      printf("keyboard event close \n");
    }
    
}

I would be very glad if somebody can help me :frowning:

You do not need to redeclare the lv_ta_get_text function. lvgl.h already declares it.

This will print the contents of the text area, followed by a newline:

printf("%s\n", lv_ta_get_text(ta));

Hey there. Thank you for your time and your answer. You guys are really awesome.

Sadly this does end with a compiler errror ( " ta was not declared in this scope").
Did i miss to add or declare ta somewhere?
This code is used for creating keyboard and textarea :

                  /*Create styles for the keyboard*/
    static lv_style_t rel_style, pr_style;

    lv_style_copy(&rel_style, &lv_style_btn_rel);
    rel_style.body.radius = 0;
    rel_style.body.border.width = 1;

    lv_style_copy(&pr_style, &lv_style_btn_pr);
    pr_style.body.radius = 0;
    pr_style.body.border.width = 1;

    /*Create a keyboard and apply the styles*/
    lv_obj_t *kb = lv_kb_create(lv_scr_act(), NULL);
    lv_obj_set_event_cb(kb, keyboard_event_cb);
    lv_kb_set_cursor_manage(kb, true);
    lv_kb_set_style(kb, LV_KB_STYLE_BG, &lv_style_transp_tight);
    lv_kb_set_style(kb, LV_KB_STYLE_BTN_REL, &rel_style);
    lv_kb_set_style(kb, LV_KB_STYLE_BTN_PR, &pr_style);

    /*Create a text area. The keyboard will write here*/
    lv_obj_t *ta = lv_ta_create(lv_scr_act(), NULL);
    lv_obj_align(ta, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);
    lv_ta_set_text(ta, "berbelberbelberbel");
    lv_obj_set_event_cb(ta, keyboard_event_cb);

    /*Assign the text area to the keyboard*/
    lv_kb_set_ta(kb, ta);

This is my event code ( with you answer included) did i miss to add ta somewhere?

static void keyboard_event_cb(lv_obj_t * kb, lv_event_t event)
{
    lv_kb_def_event_cb(kb, event);

    if(event == LV_EVENT_APPLY)
      { 
        printf("%s\n", lv_ta_get_text(ta));
        printf("keyboard event apply \n");
        lv_obj_del(kb);
        kb = NULL;
        
        

     }
    
    if(event == LV_EVENT_CANCEL)
    {
      lv_obj_del(kb);
      kb = NULL;
      printf("keyboard event apply \n");
    }
    
}

ta is the return value of lv_ta_create. You should store that in a global variable (or some other variable that is in scope).

I can see that ta is already in your code to create the text area; I leave it as an exercise to you to make it visible to the other function as well.

1 Like

Ok thank you very much for your hint. It is working now. I wrote lv_obj_t *ta; at the begining of my sourcecode, changed

lv_obj_t *ta = lv_ta_create(lv_scr_act(), NULL);

to

ta = lv_ta_create(lv_scr_act(), NULL);

And now printf("%s\n", lv_ta_get_text(ta)); is printing the desired results <3 <3

Even killing the textarea now works like a charm. Thank you very much @embeddedt!

Glad to help!