Labs / LVGL / 03 Quick Start Example - Control Section (Based on Web Simulator and LVGL v9.0)

3. Matrix Keypad

Author: Li Zisheng · Source: FreakStudio open documentation, v1.0 · License: CC BY 4.0

Sample Code:

import display_driver
import lvgl as lv

lv.init()

#Get the currently active screen object
scr = lv.screen_active()
#Set the screen background color to black
scr.set_style_bg_color(lv.color_hex(0x000000), 0)

#Create a button matrix
btnm = lv.buttonmatrix(scr)
#Set 3 buttons
btnm_map = ["A1", "A2", "A3"]
btnm.set_map(btnm_map)
#Center it
btnm.center()

#Interaction callback function
def btnm_event_cb(e):
    #Get the index of the clicked button
    pressed_btn = btnm.get_selected_button()  
    if pressed_btn != -1:
        #Get the text of the button at the corresponding index
        text_btn = btnm.get_button_text(pressed_btn)
        print(f"Button {text_btn} is clicked")  # Only this line is modified, output in English only
#Register the interaction callback function. For more states see the "class EVENT" section of api.py
btnm.add_event_cb(btnm_event_cb, lv.EVENT.CLICKED, None)

# Load the screen
lv.screen_load(scr)
A1 A2 A3
A1 A2 A3

First, initialize the LVGL core engine, obtain the currently active screen object and set its background color to black. Next, create a matrix button control containing three buttons "A1", "A2" and "A3" and make it displayed centered on the screen. Bind a click event callback function to this matrix button: when any button is clicked, acquire the index and corresponding text of the clicked button, and print a fully English button click prompt in the Console (e. g., "Button A1 is clicked"). Finally, load the screen to complete the rendering of the entire interface.

Version Record

Version Number

Modifier

Time

Content

v1.0

Li Zisheng

2026/03/23

Initialize the document.