Keyboard (lv_keyboard)
An on-screen virtual keyboard, a specialized Button Matrix.
Overview
The Keyboard is a specialized Button Matrix with predefined keymaps that provides an on-screen virtual keyboard. It can be attached to a Text Area so key presses edit its text.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_keyboard.c
*/
#include "../lv_examples.h"
/**
* @title Keyboard
* @brief A keyboard paired with a text area, with key popovers enabled.
*
* Keyboard and Text Area share the screen; LVGL's default focus group
* routes key presses to the focused input, so typing lands in the field
* above. `mode` selects the starting layout and `popovers="true"` shows the
* enlarged key preview while a key is pressed.
*/
void lv_example_keyboard(void)
{
static lv_style_t style_bg;
static lv_style_t style_btn_normal;
static lv_style_t style_btn_checked;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_bg);
lv_style_init(&style_btn_normal);
lv_style_init(&style_btn_checked);
lv_style_set_bg_color(&style_bg, lv_color_hex(0x111827));
lv_style_set_text_color(&style_bg, lv_color_hex(0xf3f4f6));
lv_style_set_radius(&style_bg, 8);
lv_style_set_pad_all(&style_bg, 8);
lv_style_set_border_color(&style_bg, lv_color_hex(0x3b82f6));
lv_style_set_border_width(&style_bg, 1);
lv_style_set_bg_color(&style_btn_normal, lv_color_hex(0x515e7b));
lv_style_set_text_color(&style_btn_normal, lv_color_hex(0xf3f4f6));
lv_style_set_radius(&style_btn_normal, 6);
lv_style_set_bg_color(&style_btn_checked, lv_color_hex(0x3b82f6));
lv_style_set_text_color(&style_btn_checked, lv_color_hex(0xf3f4f6));
lv_style_set_radius(&style_btn_checked, 6);
inited = true;
}
lv_obj_t * screen = lv_screen_active();
/* 💡 Tap keys — they type into the field above and show a popover preview. */
lv_obj_t * ta_input = lv_textarea_create(screen);
lv_obj_set_width(ta_input, 300);
lv_textarea_set_one_line(ta_input, true);
lv_textarea_set_placeholder_text(ta_input, "Tap the keyboard ...");
lv_obj_set_align(ta_input, LV_ALIGN_TOP_MID);
lv_obj_set_y(ta_input, 30);
lv_obj_add_style(ta_input, &style_bg, 0);
lv_obj_t * lv_keyboard_0 = lv_keyboard_create(screen);
lv_obj_set_size(lv_keyboard_0, 320, 160);
lv_keyboard_set_mode(lv_keyboard_0, LV_KEYBOARD_MODE_TEXT_LOWER);
lv_keyboard_set_popovers(lv_keyboard_0, true);
lv_keyboard_set_textarea(lv_keyboard_0, ta_input);
lv_obj_set_align(lv_keyboard_0, LV_ALIGN_BOTTOM_MID);
lv_obj_add_style(lv_keyboard_0, &style_bg, 0);
lv_obj_add_style(lv_keyboard_0, &style_btn_normal, LV_PART_ITEMS);
lv_obj_add_style(lv_keyboard_0, &style_btn_checked, LV_PART_ITEMS | LV_STATE_CHECKED);
}
<!--
@title Keyboard
@brief A keyboard paired with a text area, with key popovers enabled.
Keyboard and Text Area share the screen; LVGL's default focus group
routes key presses to the focused input, so typing lands in the field
above. `mode` selects the starting layout and `popovers="true"` shows the
enlarged key preview while a key is pressed.
-->
<screen>
<styles>
<style
name="style_bg"
bg_color="0x111827"
text_color="0xf3f4f6"
radius="8"
pad_all="8"
border_color="0x3b82f6"
border_width="1"
/>
<style name="style_btn_normal" bg_color="0x515e7b" text_color="0xf3f4f6" radius="6" />
<style name="style_btn_checked" bg_color="0x3b82f6" text_color="0xf3f4f6" radius="6" />
</styles>
<view>
<!-- 💡 Tap keys — they type into the field above and show a popover preview. -->
<lv_textarea
name="ta_input"
width="300"
one_line="true"
placeholder_text="Tap the keyboard ..."
align="top_mid"
y="30"
>
<style name="style_bg" />
</lv_textarea>
<lv_keyboard width="320" height="160" mode="text_lower" popovers="true" textarea="ta_input" align="bottom_mid">
<style name="style_bg" />
<style name="style_btn_normal" selector="items" />
<style name="style_btn_checked" selector="items|checked" />
</lv_keyboard>
</view>
</screen>
Enumerations
lv_keyboard_mode — Keyboard layout modes
| Value | Description |
|---|---|
text_lower | Show lowercase letters |
text_upper | Show uppercase letters |
special | Show special characters and symbols |
number | Show numeric keys |
user_1 | User-defined layout 1 |
user_2 | User-defined layout 2 |
user_3 | User-defined layout 3 |
user_4 | User-defined layout 4 |
text_arabic | Show Arabic characters |
Properties
Parts
Style parts of the widget with local style (e.g. style_bg_color-knob="0xff0000") or with style sheets (e.g. <style name="style_knob" selector="knob">). See Styles to learn more.
| Part | Description |
|---|---|
main | The background behind the keys (extends lv_buttonmatrix): background properties. pad_row and pad_column set the gaps between keys. |
items | The keys: text and background properties. Combine with the 'checked', 'pressed', 'focused' and 'disabled' states, e.g. items|pressed. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
mode | enum:lv_keyboard_mode | Set the keyboard layout |
popovers | bool | Show enlarged key previews on press |
textarea | lv_obj | Connect a textare to the keyboard |
Last updated on