How to get coordinates


how to get coordinates touchscreens and when you click mouse

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


What LVGL version are you using?


What do you want to achieve?

get coordinates

You can use a custom signal handler. The button matrix is a good example.

the example is certainly good, but I don’t understand how I get the coordinates, I just want to click anywhere on the screen and get x and y

You can create a fullscreen object and then use the method I linked to get the coordinates when you click on the screen.

ok, i`m create object like this

    lv_obj_t * obj1;
    obj1 = lv_obj_create(lv_scr_act(), NULL);
    lv_obj_set_size(obj1, 100, 50);
    lv_obj_set_event_cb(obj1, lv_signal);

lv_res_t lv_btnsignal(lv_obj_t * obj, lv_event_t event){
    lv_point_t p;
    if(event == LV_SIGNAL_PRESSING) {
//        uint16_t btn_pr = LV_BTNMATRIX_BTN_NONE;
        /*Search the pressed area*/
        lv_indev_t *indev = lv_indev_get_act();
        lv_indev_type_t indev_type = lv_indev_get_type(indev);
        if (indev_type == LV_INDEV_TYPE_ENCODER || indev_type == LV_INDEV_TYPE_KEYPAD) return LV_RES_OK;

        lv_indev_get_point(indev, &p);

this is correctly? if not correctly, whats change?

That should be correct, but if you want to detect clicks on the whole screen you would need to make the object screen-sized.

I understand it…
ok, how print, function lv_indev_get_point there is nothing return

The coordinates are stored in p after you call that function.

thanks))) i can apply to any object?

Yes, but keep in mind that if you use a custom signal handler on other objects, you will want to save the previous signal handler and call through to it, otherwise the object’s functionality will be lost.

1 Like

I understand it, thank you)

@embeddedt I have the same question but how to solve it in v8 of lvlg please? I cannot see such method on v8 in link you shared. Thanks.

OK figured it out by myself. Maybe it helps somebody:

    static void lv_custom_event(const lv_obj_class_t * class_p, lv_event_t * e)

    lv_result_t res;

    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    lv_buttonmatrix_t * btnm = (lv_buttonmatrix_t *)obj;
    lv_point_t p;

    if(code == LV_EVENT_PRESSED) {
        lv_indev_t * indev = lv_event_get_indev(e);

        lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_active());
        if(indev_type == LV_INDEV_TYPE_POINTER || indev_type == LV_INDEV_TYPE_BUTTON) {
            /*Search the pressed area*/
            lv_indev_get_point(indev, &p);
            printf("X:%d, Y:%d\n", p.x, p.y);

    lv_obj_t * obj1;
    obj1 = lv_obj_create(lv_screen_active());
    lv_obj_set_size(obj1, 500, 300);
    lv_obj_add_event(obj1, lv_custom_event, 0, NULL);