如题 能接受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;
}
}
离线
按照你说的找到了 感谢指点
离线