Events

Events are triggered in LVGL when something happens which might be interesting to the user, e.g.

Edit on GitHub

Events are triggered in LVGL when something happens which might be interesting to the user, e.g. when a Widget:

  • is clicked
  • is scrolled
  • has its value changed
  • is redrawn, etc.

Besides Widgets, events can be registered for displays and input devices as well. It is not detailed below, but you can do this by changing the prefix of the functions from lv_obj_ to lv_display_ or lv_indev_.

Adding Events to a Widget

The user can assign callback functions to a widget to process events. In practice, it looks like this:

 
lv_obj_t * btn = lv_button_create(lv_screen_active());
lv_obj_add_event_cb(btn, my_event_cb, LV_EVENT_CLICKED, user_data);   /* Assign an event callback */

...

static void my_event_cb(lv_event_t * event)
{
    printf("Clicked\n");
}

In the example LV_EVENT_CLICKED means that only the click event will call my_event_cb. See the list of event codes for all the options. LV_EVENT_ALL can be used to receive all events.

The last parameter of lv_obj_add_event_cb is a pointer to any custom data that will be available in the event. NULL may be passed for this argument if there is no need to use that data when the event is processed. You can retrieve the pointer passed when setting the callback function like this:

 
my_user_data_t  * user_data;
...
user_data = lv_event_get_user_data(e);

More events can be added to a Widget, like this:

 
lv_obj_add_event_cb(widget, my_event_cb_1, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(widget, my_event_cb_2, LV_EVENT_PRESSED, NULL);
lv_obj_add_event_cb(widget, my_event_cb_3, LV_EVENT_ALL, NULL);       /* No filtering, receive all events */

Even the same event callback can be used on a Widget with different user_data. For example:

 
lv_obj_add_event_cb(widget, increment_on_click, LV_EVENT_CLICKED, &num1);
lv_obj_add_event_cb(widget, increment_on_click, LV_EVENT_CLICKED, &num2);

The events will be called in the order as they were added.

Other Widgets can use the same event callback.

In the very same way, events can be attached to input devices and displays like this:

 
lv_display_add_event_cb(disp, event_cb, LV_EVENT_RESOLUTION_CHANGED, NULL);
lv_indev_add_event_cb(indev, event_cb, LV_EVENT_CLICKED, NULL);

Removing Event(s) from Widgets

 
uint32_t i;
uint32_t event_cnt = lv_obj_get_event_count(widget);
for(i = 0; i < event_cnt; i++) {
    lv_event_dsc_t * event_dsc = lv_obj_get_event_dsc(widget, i);
    if(lv_event_dsc_get_cb(event_dsc) == some_event_cb) {
        lv_obj_remove_event(widget, i);
        break;
    }
}

Event Codes

The event codes can be grouped into these categories: - Input device events - Drawing events - Other events - Special events - Custom events

All Widgets (such as Buttons/Labels/Sliders etc.) regardless their type receive the Input device, Drawing and Other events.

However, the Special events are specific to a particular widget type. See the widgets' documentation to learn when they are sent,

Custom events are added by the user and are never sent by LVGL.

The following event codes exist:

Input Device Events

Drawing Events

In drawing-event callback functions the rendering sequence has already begun, and during this period, making changes to any Widget's attributes, styles, or creating/deleting widgets is forbidden. If you attempt to do so, LVGL will generate an assertion failure with a message indicating that invalidating an area is not allowed during rendering.

Special Events

Other Events

Display Events

Custom Events

Any number of custom event codes can be registered by uint32_t MY_EVENT_1 = lv_event_register_id

They can be sent to any Widget with

lv_obj_send_event(widget, MY_EVENT_1, &some_data)

Refresh Event

LV_EVENT_REFRESH is a special event because it's designed to let the user notify a Widget to refresh itself. Some examples:

  • notify a label to refresh its text according to one or more variables (e.g. current time)
  • refresh a label when the language changes
  • enable a button if some conditions are met (e.g. the correct PIN is entered)
  • add/remove styles to/from a Widget if a limit is exceeded, etc

Sending Events Manually

To manually send events to a Widget, use lv_obj_send_event(widget, <EVENT_CODE>, &some_data).

For example, this can be used to manually close a message box by simulating a button press (although there are simpler ways to do this):

 
/* Simulate the press of the first button (indexes start from zero) */
uint32_t btn_id = 0;
lv_obj_send_event(mbox, LV_EVENT_VALUE_CHANGED, &btn_id);

The same works for display and input devices with lv_display_send_event(widget, <EVENT_CODE>, &some_data) and lv_indev_send_event(widget, <EVENT_CODE>, &some_data).

Fields of lv_event_t

lv_event_t is the only parameter passed to the event callback and it contains all data about the event. The following values can be retrieved from it:

When using C++, prefer lv_event_get_target_obj(e) over lv_event_get_target(e) when you know the target is a Widget, as it returns the correct type without requiring a cast.

Only call lv_event_get_target_obj(e) when the event target is known to be a Widget. Calling it for Display or Indev targets is considered Undefined Behavior.

Event Bubbling

If lv_obj_add_flag(widget, LV_OBJ_FLAG_EVENT_BUBBLE) is enabled all events will be sent to a Widget's parent as well. If the parent also has LV_OBJ_FLAG_EVENT_BUBBLE enabled the event will be sent to its parent, and so on.

The target parameter of the event is always the current target Widget, not the original Widget. To get the original target call lv_event_get_target_obj(e) in the event handler.

Event Trickle

Also known as Event Capturing, if lv_obj_add_flag(widget, LV_OBJ_FLAG_EVENT_TRICKLE) is enabled all events will be sent to the Widget's children as well. This is the opposite of event bubbling --- instead of propagating up the parent, events propagate down to the children.

The trickle mechanism only affects immediate children, not grandchildren or deeper descendants. If you need events to propagate to deeper levels, each child would need to have the LV_OBJ_FLAG_EVENT_TRICKLE flag enabled.

Like with bubbling, the target parameter of the event is always the current target Widget, not the original Widget. To get the original target call lv_event_get_target_obj(e) in the event handler.

Examples

Button click event

Click streaks

Handle multiple events

Event bubbling

Event trickle-down

Draw event

API

How is this guide?

Last updated on

On this page