Input devices(输入设备)
显示原文
An input device usually means:
Pointer-like input device like touchpad or mouse
Keypads like a normal keyboard or simple numeric keypad
Encoders with left/right turn and push options
External hardware buttons which are assigned to specific points on the screen
- important:
Before reading further, please read the Porting section of Input devices
输入设备通常指:
像触摸板或鼠标这样的类似指针的输入设备
像普通键盘或简单的数字小键盘这样的键盘
左/右旋转和按下选项的编码器
被分配到屏幕特定位置的外部硬件按钮
- important:
在继续阅读前,请阅读「输入设备」的 移植 部分
Pointers(光标)
Cursor(光标)
显示原文
Pointer input devices (like a mouse) can have a cursor.
...
lv_indev_t * mouse_indev = lv_indev_create();
...
LV_IMAGE_DECLARE(mouse_cursor_icon); /*Declare the image source.*/
lv_obj_t * cursor_obj = lv_image_create(lv_screen_active()); /*Create an image object for the cursor */
lv_image_set_src(cursor_obj, &mouse_cursor_icon); /*Set the image source*/
lv_indev_set_cursor(mouse_indev, cursor_obj); /*Connect the image object to the driver*/
Note that the cursor object should have lv_obj_remove_flag(cursor_obj, LV_OBJ_FLAG_CLICKABLE). For images, clicking is disabled by default.
指针输入设备(如鼠标)可以有一个指针。
代码示例:
...
lv_indev_t *mouse_indev = lv_indev_create();
...
LV_IMG_DECLARE(mouse_cursor_icon); /* 声明图片源 */
lv_obj_t *cursor_obj = lv_image_create(lv_screen_active()); /* 为光标创建一个图片对象 */
lv_image_set_src(cursor_obj, &mouse_cursor_icon); /* 设置图片源 */
lv_indev_set_cursor(mouse_indev, cursor_obj); /* 将图片对象与驱动程序连接 */
注意,光标对象应该有这样的设置 lv_obj_remove_flag(cursor_obj, LV_OBJ_FLAG_CLICKABLE)。对于图片,默认情况下是不能 点击 的。
Gestures (手势)
显示原文
Pointer input devices can detect basic gestures. By default, most of the
widgets send the gestures to its parent, so finally the gestures can be
detected on the screen object in a form of an LV_EVENT_GESTURE
event. For example:
void my_event(lv_event_t * e)
{
lv_obj_t * screen = lv_event_get_current_target(e);
lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_active());
switch(dir) {
case LV_DIR_LEFT:
...
break;
case LV_DIR_RIGHT:
...
break;
case LV_DIR_TOP:
...
break;
case LV_DIR_BOTTOM:
...
break;
}
}
...
lv_obj_add_event_cb(screen1, my_event, LV_EVENT_GESTURE, NULL);
To prevent passing the gesture event to the parent from an object use lv_obj_remove_flag(obj, LV_OBJ_FLAG_GESTURE_BUBBLE).
Note that, gestures are not triggered if an object is being scrolled.
If you did some action on a gesture you can call lv_indev_wait_release(lv_indev_active()) in the event handler to prevent LVGL sending further input device related events.
指针输入设备可以检测基本手势。默认情况下,大多数控件将手势发送给其父对象,因此最终手势可以以 LV_EVENT_GESTURE
事件的形式在屏幕对象上被检测到。例如:
void my_event(lv_event_t * e)
{
lv_obj_t * screen = lv_event_get_current_target(e);
lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_active());
switch(dir) {
case LV_DIR_LEFT:
...
break;
case LV_DIR_RIGHT:
...
break;
case LV_DIR_TOP:
...
break;
case LV_DIR_BOTTOM:
...
break;
}
}
...
lv_obj_add_event_cb(screen1, my_event, LV_EVENT_GESTURE, NULL);
要阻止将手势事件从对象传递给其父对象,请使用 lv_obj_remove_flag(obj, LV_OBJ_FLAG_GESTURE_BUBBLE)。
注意,如果对象正在滚动,手势不会触发。
如果您在手势上执行了某个操作,可以在事件处理程序中调用 lv_indev_wait_release(lv_indev_active()) 来防止 LVGL 发送进一步的与输入设备相关的事件。
Keypad and encoder(键盘和编码器)
显示原文
You can fully control the user interface without a touchpad or mouse by using a keypad or encoder(s). It works similar to the TAB key on the PC to select an element in an application or a web page.
您可以通过使用键盘或编码器来完全控制用户界面,而无需触摸板或鼠标。它类似于在电脑上使用 TAB 键来选择应用程序或网页中的元素。
Groups(组)
显示原文
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. For example, if a Text area is focused and you press some letter on a keyboard, the keys will be sent and inserted into the text area. Similarly, if a Slider is focused and you press the left or right arrows, the slider's value will be changed.
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).
你想要使用键盘或编码器控制的对象需要添加到一个 Group 中。每个组中只有一个焦点对象,它接收按键或编码器动作。例如,如果一个 文本区域 被聚焦,你按键盘上的字母,这些按键会被发送并插入到文本区域中。同样,如果一个 滑块 被聚焦,你按下左右箭头键,滑块的数值会改变。
你需要将输入设备与一个组关联起来。一个输入设备只能发送按键事件到一个组,但一个组可以接收来自多个输入设备的数据。
使用 lv_group_t * g = lv_group_create() 来创建一个组,使用 lv_group_add_obj(g, obj) 来将一个对象添加到组中。
使用 lv_indev_set_group(indev, g) 来将一个组与一个输入设备关联起来。
Keys(按键)
显示原文
There are some predefined keys which have special meaning:
LV_KEY_NEXT
: Focus on the next objectLV_KEY_PREV
: Focus on the previous objectLV_KEY_ENTER
: TriggersLV_EVENT_PRESSED
,LV_EVENT_CLICKED
, orLV_EVENT_LONG_PRESSED
etc. eventsLV_KEY_UP
: Increase value or move upwardsLV_KEY_DOWN
: Decrease value or move downwardsLV_KEY_RIGHT
: Increase value or move to the rightLV_KEY_LEFT
: Decrease value or move to the leftLV_KEY_ESC
: Close or exit (E.g. close a Drop down list)LV_KEY_DEL
: Delete (E.g. a character on the right in a Text area)LV_KEY_BACKSPACE
: Delete a character on the left (E.g. in a Text area)LV_KEY_HOME
: Go to the beginning/top (E.g. in a Text area)LV_KEY_END
: Go to the end (E.g. in a Text area)
The most important special keys in your read_cb()
function are:
You should translate some of your keys to these special keys to support navigation in a group and interact with selected objects.
Usually, it's enough to use only LV_KEY_LEFT
and LV_KEY_RIGHT
because most
objects can be fully controlled with them.
With an encoder you should use only LV_KEY_LEFT
, LV_KEY_RIGHT
,
and LV_KEY_ENTER
.
有一些预定义的键具有特殊含义:
LV_KEY_NEXT
:焦点移至下一个对象LV_KEY_PREV
:焦点移至上一个对象LV_KEY_ENTER
:触发LV_EVENT_PRESSED
、LV_EVENT_CLICKED
或LV_EVENT_LONG_PRESSED
等事件LV_KEY_UP
:增加值或向上移动LV_KEY_DOWN
:减少值或向下移动LV_KEY_RIGHT
:增加值或向右移动LV_KEY_LEFT
:减少值或向左移动LV_KEY_ESC
:关闭或退出(例如,关闭 下拉列表)LV_KEY_DEL
:删除(例如,在 文本区 中删除右侧的字符)LV_KEY_BACKSPACE
:删除左侧的一个字符(例如,在 文本区 中)LV_KEY_HOME
:跳至开头/顶部(例如,在 文本区 中)LV_KEY_END
:跳至末尾(例如,在 文本区 中)
在你的 read_cb()
函数中,最重要的特殊键是:
你应该将一些键转换为这些特殊键,以支持在组内导航和与选定对象交互。
通常,只使用 LV_KEY_LEFT
和 LV_KEY_RIGHT
就足够了,因为大多数对象可以完全通过它们进行控制。
在使用编码器时,只需使用 LV_KEY_LEFT
、LV_KEY_RIGHT
和 LV_KEY_ENTER
。
Default group(默认组)
显示原文
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)
Don't forget to assign one or more input devices to the default group with lv_indev_set_group(my_indev, g).
交互式控件,比如按钮、复选框、滑块等,可以自动添加到默认组中。只需创建一个组 lv_group_t * g = lv_group_create() 并将默认组设为 :g:cpp:expr:`lv_group_set_default(g)`。
不要忘记用 lv_indev_set_group(my_indev, g) 将一个或多个输入设备分配给默认组。
Styling(风格样式)
显示原文
If an object is focused either by clicking it via touchpad or focused
via an encoder or keypad it goes to the LV_STATE_FOCUSED
state.
Hence, focused styles will be applied to it.
If an object switches to edit mode it enters the LV_STATE_FOCUSED | LV_STATE_EDITED states so these style properties will be shown.
For a more detailed description read the Style section.
如果一个对象通过触摸板点击或者编码器或键盘被聚焦,它会进入 LV_STATE_FOCUSED
状态。因此,聚焦的样式将被应用于该对象。
如果一个对象切换到编辑模式,它会进入:cpp:expr:LV_STATE_FOCUSED | LV_STATE_EDITED 状态,因此这些样式属性将会显示出来。
要获取更详细的描述,请阅读 Style 部分。