Skip to content

实体按键

使用一个实体按键的时候, 需要把所控制的组件添加到组里面, 把他和按键的组关联起来

image-20240603184238603

有的部件默认已经在这一个组里面了, 不需要再次添加

配置输入设备

在这里配置时候实际使用的是键盘的相关代码

c
/* 键盘 */
static void keypad_init(void);
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static uint32_t keypad_get_key(void);

image-20240603184852216

c
/*------------------
 * 键盘
 * -----------------*/

/**
 * @brief       初始化键盘
 * @param
 * @retval
 */
static void keypad_init(void)
{
    /*Your code comes here*/
    key_init();
}

/**
 * @brief       图形库的键盘读取回调函数
 * @param       indev_drv : 键盘设备
 *   @arg       data      : 输入设备数据结构体
 * @retval
 */
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static uint32_t last_key = 0;

//    /* 这段代码是 LVGL 给出的例子,这里获取坐标好像是多余的 */
//    /*Get the current x and y coordinates*/
//    mouse_get_xy(&data->point.x, &data->point.y);

    /* 获取按键是否被按下,并保存键值 */
    uint32_t act_key = keypad_get_key();
    if(act_key != 0) {
        data->state = LV_INDEV_STATE_PR;

        /* 将键值转换成 LVGL 的控制字符 */
        switch(act_key) {
        case KEY0_PRES:
            act_key = LV_KEY_NEXT;
            break;
        case KEY1_PRES:
            act_key = LV_KEY_PREV;
            break;
        case WKUP_PRES:
            act_key = LV_KEY_ENTER;
            break;
        }

        last_key = act_key;
    } else {
        data->state = LV_INDEV_STATE_REL;
    }

    data->key = last_key;
}

/**
 * @brief       获取当前正在按下的按键
 * @param
 * @retval      0 : 按键没有被按下
 */
static uint32_t keypad_get_key(void)
{
    /*Your code comes here*/

    return key_scan(0);
}
  • 支持的按键
c
enum {
    LV_KEY_UP        = 17,  /*0x11*/
    LV_KEY_DOWN      = 18,  /*0x12*/
    LV_KEY_RIGHT     = 19,  /*0x13*/
    LV_KEY_LEFT      = 20,  /*0x14*/
    LV_KEY_ESC       = 27,  /*0x1B*/
    LV_KEY_DEL       = 127, /*0x7F*/
    LV_KEY_BACKSPACE = 8,   /*0x08*/
    LV_KEY_ENTER     = 10,  /*0x0A, '\n'*/
    LV_KEY_NEXT      = 9,   /*0x09, '\t'*/
    LV_KEY_PREV      = 11,  /*0x0B, '*/
    LV_KEY_HOME      = 2,   /*0x02, STX*/
    LV_KEY_END       = 3,   /*0x03, ETX*/
};

在文档的Key这一个部分有某一个部件可以接受的按键

c
/*------------------
 * Keypad
 * -----------------*/

/*Initialize your keypad*/
static void keypad_init(void)
{
    /*Your code comes here*/
}

/*Will be called by the library to read the mouse*/
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static uint32_t last_key = 0;

    /*Get the current x and y coordinates*/
    mouse_get_xy(&data->point.x, &data->point.y);

    /*Get whether the a key is pressed and save the pressed key*/
    uint32_t act_key = keypad_get_key();
    if(act_key != 0) {
        data->state = LV_INDEV_STATE_PR;

        /*Translate the keys to LVGL control characters according to your key definitions*/
        switch(act_key) {
        case 1:
            act_key = LV_KEY_NEXT;
            break;
        case 2:
            act_key = LV_KEY_PREV;
            break;
        case 3:
            act_key = LV_KEY_LEFT;
            break;
        case 4:
            act_key = LV_KEY_RIGHT;
            break;
        case 5:
            act_key = LV_KEY_ENTER;
            break;
        }

        last_key = act_key;
    } else {
        data->state = LV_INDEV_STATE_REL;
    }

    data->key = last_key;
}

/*Get the currently being pressed key.  0 if no key is pressed*/
static uint32_t keypad_get_key(void)
{
    /*Your code comes here*/

    return 0;
}

设置组

image-20240603190016150

Objects you want to control with a keypad or encoder need to be added to a Group. In every group there is exactly one focused object which receives the pressed keys or the encoder actions.

You need to associate an input device with a group. An input device can send key events to only one group but a group can receive data from more than one input device.

To create a group use lv_group_t * g = lv_group_create() and to add an object to the group use lv_group_add_obj(g, obj). 创建

To associate a group with an input device use lv_indev_set_group(indev, g), where indev is the return value of lv_indev_drv_register() 设置关联设备

Interactive widgets - such as buttons, checkboxes, sliders, etc. - can be automatically added to a default group. Just create a group with lv_group_t * g = lv_group_create(); and set the default group with lv_group_set_default(g); 可以使用默认的设置