键盘(lv_keyboard)对象是一个特殊的按钮矩阵(lv_btnmatrix),自身实现了按钮(map)映射和其他功能,目的是用于实现一个虚拟键盘将文本写入文本框(lv_textarea)。

1.常用方法

创建keyboard:

  1. lv_obj_t * lv_keyboard_create(lv_obj_t * parent)

关联textarea到keyboard:

  1. void lv_keyboard_set_textarea(lv_obj_t * obj, lv_obj_t * ta)

2.代码实现

  1. static void ta_event_cb(lv_event_t * e)
  2. {
  3. lv_event_code_t code = lv_event_get_code(e);
  4. lv_obj_t * ta = lv_event_get_target(e);
  5. lv_obj_t * kb = lv_event_get_user_data(e);
  6. if(code == LV_EVENT_FOCUSED) {
  7. lv_keyboard_set_textarea(kb, ta);
  8. lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
  9. }
  10. if(code == LV_EVENT_DEFOCUSED) {
  11. lv_keyboard_set_textarea(kb, NULL);
  12. lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
  13. }
  14. }
  15. void keyboard_1(void)
  16. {
  17. /*Create a keyboard to use it with an of the text areas*/
  18. lv_obj_t *kb = lv_keyboard_create(lv_scr_act());
  19. /*Create a text area. The keyboard will write here*/
  20. lv_obj_t * ta;
  21. ta = lv_textarea_create(lv_scr_act());
  22. lv_obj_align(ta, LV_ALIGN_TOP_LEFT, 10, 10);
  23. lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
  24. lv_textarea_set_placeholder_text(ta, "Hello");
  25. lv_obj_set_size(ta, 140, 80);
  26. ta = lv_textarea_create(lv_scr_act());
  27. lv_obj_align(ta, LV_ALIGN_TOP_RIGHT, -10, 10);
  28. lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
  29. lv_obj_set_size(ta, 140, 80);
  30. }

效果:
48.png