Button click event not recognized

Hi.

I’m using lvgl 9.2.2 with esp idf 5.4.2. I’m trying to use a simple button to count click event. I use the example, modified, but it doesn’t, work. The touch is ok.

Hey, you can try my code; it’s a button with a counter when clicked. I hope this helps. But in general, post your code, otherwise it’s hard to understand what the problem is.

static void btn_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * btn = lv_event_get_target_obj(e);
if(code == LV_EVENT_CLICKED) {
static uint8_t cnt = 0;
cnt++;
/Get the first child of the button which is the label and change its text/
lv_obj_t * label = lv_obj_get_child(btn, 0);
lv_label_set_text_fmt(label, “Button: %d”, cnt);
}
}

/**

  • Create a button with a label and react on click event.
    */
    void lv_example_get_started_2(void)
    {
    lv_obj_t * btn = lv_button_create(lv_screen_active()); /Add a button the current screen/
    lv_obj_set_pos(btn, 10, 10); /Set its position/
    lv_obj_set_size(btn, 120, 50); /Set its size/
    lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL); /Assign a callback to the button/

    lv_obj_t * label = lv_label_create(btn); /Add a label to the button/
    lv_label_set_text(label, “Button”); /Set the labels text/
    lv_obj_center(label);
    }
    void app_main(void){
    lvgl_port_lock(0);
    lv_example_get_started_2();
    lvgl_port_unlock();
    }

@tsebelev

I used the same code in the example on LVGL docs page, i.e. your code. It doesn’t work.
Attached there is my project. I’m sure that touch works because I print the position of the pen when touches display.

esp32displayPrj.zip (43.2 KB)