MantisIDE

Events and gestures · Round 1.28" touch watch

Guide by MantisIDE Team · Fri Nov 27 2026 · 0 points · 0 comments

Widget gallery 14 - Events: gestures, dragging an object, long press, touch position

You’ll learn: drag, swipe, long press, touch point

Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Widget gallery. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.

# Widget gallery 14 - Events: gestures, dragging an object, long press, touch position
import lvgl as lv
import display

display.init()                     # start the display and touch (does nothing if already on)

BG = 0x05110A
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
RED = 0xFF6B6B
keep = []                             # styles, buffers and callbacks LVGL uses must stay referenced

FONTS = {12: lv.font_montserrat_12, 14: lv.font_montserrat_14, 16: lv.font_montserrat_16}


def label(parent, text="", color=TEXT, font=16, align=lv.ALIGN.CENTER, x=0, y=0, zoom=0):
    """lv.label with a colour and a Montserrat 12/14/16 font. zoom=2 draws it twice as big."""
    lb = lv.label(parent)
    lb.set_text(text)
    lb.set_style_text_color(lv.color_hex(color), 0)
    lb.set_style_text_font(FONTS.get(font, lv.font_montserrat_16), 0)
    if zoom:
        lb.set_style_transform_scale(int(256 * zoom), 0)   # 256 = 100 %
        lb.update_layout()
        lb.set_style_transform_pivot_x(lb.get_width() // 2, 0)
        lb.set_style_transform_pivot_y(lb.get_height() // 2, 0)
    lb.align(align, int(x), int(y))
    return lb


def dot(parent, size, color, x=0, y=0, align=lv.ALIGN.CENTER):
    """A filled circle: a plain lv.obj with every theme style removed."""
    o = lv.obj(parent)
    o.remove_style_all()
    size = max(1, int(size))
    o.set_size(size, size)
    o.set_style_radius(lv.RADIUS_CIRCLE, 0)
    o.set_style_bg_color(lv.color_hex(color), 0)
    o.set_style_bg_opa(lv.OPA.COVER, 0)
    o.remove_flag(lv.obj.FLAG.CLICKABLE)
    o.align(align, int(x), int(y))
    return o


def on_swipe(obj, cb):
    """cb('left' | 'right' | 'up' | 'down') when a finger swipes across obj."""
    names = {lv.DIR.LEFT: "left", lv.DIR.RIGHT: "right", lv.DIR.TOP: "up", lv.DIR.BOTTOM: "down"}

    def handler(e):
        d = lv.indev_active().get_gesture_dir()
        if d in names:
            cb(names[d])

    obj.add_event_cb(handler, lv.EVENT.GESTURE, None)
    keep.append(handler)

scr = lv.screen_active()             # the screen LVGL is showing
scr.clean()                           # remove what the last program drew
scr.set_style_bg_color(lv.color_hex(BG), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)

info = label(scr, "swipe, tap, hold or drag", MUTED, 12, y=-80)
pos = label(scr, "", TEXT, 12, y=86)

# A draggable puck: move it with your finger
puck = dot(scr, 44, ACCENT)
puck.add_flag(lv.obj.FLAG.CLICKABLE)
puck.set_style_shadow_width(18, 0)
puck.set_style_shadow_color(lv.color_hex(ACCENT), 0)


def drag(e):
    indev = lv.indev_active()
    if indev is None:
        return
    v = lv.point_t()
    indev.get_vect(v)
    puck.set_pos(puck.get_x() + v.x, puck.get_y() + v.y)

puck.add_event_cb(drag, lv.EVENT.PRESSING, None)
puck.add_event_cb(lambda e: puck.set_style_bg_color(lv.color_hex(RED), 0), lv.EVENT.LONG_PRESSED, None)
puck.add_event_cb(lambda e: puck.set_style_bg_color(lv.color_hex(ACCENT), 0), lv.EVENT.RELEASED, None)

# Swipe gestures anywhere on the background
on_swipe(scr, lambda d: info.set_text("swipe " + d))


def where(e):
    indev = lv.indev_active()
    if indev:
        p = lv.point_t()
        indev.get_point(p)
        pos.set_text("touch at %d, %d" % (p.x, p.y))

scr.add_event_cb(where, lv.EVENT.PRESSING, None)
scr.add_event_cb(lambda e: info.set_text("tap"), lv.EVENT.CLICKED, None)
print("Events used: CLICKED, PRESSING, LONG_PRESSED, RELEASED, GESTURE.")
Loading post…

Dialog