您尚未登录。

楼主 #1 2019-12-06 16:24:38

逸俊晨晖
会员
注册时间: 2018-08-29
已发帖子: 151
积分: 137.5

LittlevGL用VS2017进行PC仿真时 如何接收键盘事件

如题 能接受PC键盘的某一个按键按下的输入

离线

#2 2019-12-06 16:38:10

Zodiac
会员
注册时间: 2018-11-28
已发帖子: 42
积分: 42

Re: LittlevGL用VS2017进行PC仿真时 如何接收键盘事件

lvgl在x86平台上用SDL库来实现的跨平台兼容,能接受的输入包括键盘、鼠标和鼠标滚轮,在例程里的main.c里就有,打开相应的条件编译开关就行了。
具体的处理逻辑在indev/keyboard.c里面实现,用keyboard_read()来提取键值。
main.c

    while(1) {
        /* Periodically call the lv_task handler.
         * It could be done in a timer interrupt or an OS task too.*/
        lv_task_handler();

        usleep(5 * 1000);       /*Just to let the system breath*/

        #ifdef SDL_APPLE
            SDL_Event event;
            
            while(SDL_PollEvent(&event)) {
                #if USE_MOUSE != 0
                    mouse_handler(&event);
                #endif

                #if USE_KEYBOARD
                    keyboard_handler(&event);
                #endif

                #if USE_MOUSEWHEEL != 0
                    mousewheel_handler(&event);
                #endif
            }
        #endif

    }

keyboard.c

bool keyboard_read(lv_indev_data_t * data)
{
    data->state = state;
    data->key = keycode_to_ascii(last_key);

    return false;       /*No more data to read so return false*/
}

void keyboard_handler(SDL_Event * event)
{
    /* We only care about SDL_KEYDOWN and SDL_KEYUP events */
    switch(event->type) {
        case SDL_KEYDOWN:                       /*Button press*/
            last_key = event->key.keysym.sym;   /*Save the pressed key*/
            state = LV_INDEV_STATE_PR;          /*Save the key is pressed now*/
            break;
        case SDL_KEYUP:                         /*Button release*/
            state = LV_INDEV_STATE_REL;         /*Save the key is released but keep the last key*/
            break;
        default:
            break;

    }
}

static uint32_t keycode_to_ascii(uint32_t sdl_key)
{
    /*Remap some key to LV_GROUP_KEY_... to manage groups*/
    switch(sdl_key) {
        case SDLK_RIGHT:
        case SDLK_KP_PLUS:
            return LV_GROUP_KEY_RIGHT;

        case SDLK_LEFT:
        case SDLK_KP_MINUS:
            return LV_GROUP_KEY_LEFT;

        case SDLK_UP:
            return LV_GROUP_KEY_UP;

        case SDLK_DOWN:
            return LV_GROUP_KEY_DOWN;

        case SDLK_ESCAPE:
            return LV_GROUP_KEY_ESC;

#ifdef  LV_GROUP_KEY_BACKSPACE        /*For backward compatibility*/
        case SDLK_BACKSPACE:
            return LV_GROUP_KEY_BACKSPACE;
#endif

#ifdef  LV_GROUP_KEY_DEL        /*For backward compatibility*/
        case SDLK_DELETE:
            return LV_GROUP_KEY_DEL;
#endif
        case SDLK_KP_ENTER:
        case '\r':
            return LV_GROUP_KEY_ENTER;

        default:
            return sdl_key;
    }
}

离线

楼主 #3 2019-12-07 10:50:14

逸俊晨晖
会员
注册时间: 2018-08-29
已发帖子: 151
积分: 137.5

Re: LittlevGL用VS2017进行PC仿真时 如何接收键盘事件

按照你说的找到了 感谢指点

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn