拼音输入法

拼音输入法为键盘对象提供了中文拼音输入法(中文输入)的 API,支持 26 键和 9 键输入模式。您可以将 lv_ime_pinyin 看作是键盘对象的拼音输入法插件。

通常,只要 lv_keyboard 能运行的环境,lv_ime_pinyin 也能运行。主要有两个影响因素:字库的大小和词库的大小。

English

lv_ime_pinyin provides Chinese Pinyin input method (Chinese input) for keyboard objects, supporting 26 key and 9 key input modes. You can think of lv_ime_pinyin as a Pinyin input method plug-in for keyboard objects.

Normally, an environment where lv_keyboard can run can also run lv_ime_pinyin. There are two main influencing factors: the size of the font file and the size of the dictionary.

使用方法

lv_conf.h 中启用 LV_USE_IME_PINYIN

首先使用 lv_ime_pinyin_create(lv_scr_act()) 创建一个拼音输入法插件,然后使用 lv_ime_pinyin_set_keyboard(pinyin_ime, kb) 将您创建的 keyboard 添加到拼音输入法插件中。 您可以使用 lv_ime_pinyin_set_dict(pinyin_ime, your_dict) 来使用自定义词库。如果您一开始不想使用内置词库,可以在 lv_conf.h 中禁用 LV_IME_PINYIN_USE_DEFAULT_DICT,这可以节省大量内存空间。

内置的词库是基于 LV_FONT_SIMSUN_16_CJK 字库定制的,目前只有 1000 多个最常见的 CJK 部首,因此建议使用自定义字库和词库。

在使用拼音输入法插件的过程中,您可以随时更换键盘和词库。

English

Enable LV_USE_IME_PINYIN in lv_conf.h.

First use lv_ime_pinyin_create(lv_scr_act()) to create a Pinyin input method plug-in, then use lv_ime_pinyin_set_keyboard(pinyin_ime, kb) to add the keyboard you created to the Pinyin input method plug-in. You can use lv_ime_pinyin_set_dict(pinyin_ime, your_dict) to use a custom dictionary (if you don't want to use the built-in dictionary at first, you can disable LV_IME_PINYIN_USE_DEFAULT_DICT in lv_conf.h, which can save a lot of memory space).

The built-in thesaurus is customized based on the LV_FONT_SIMSUN_16_CJK font library, which currently only has more than 1,000 most common CJK radicals, so it is recommended to use custom fonts and thesaurus.

In the process of using the Pinyin input method plug-in, you can change the keyboard and dictionary at any time.

自定义词库

如果您不想使用内置的拼音词库,可以使用自定义词库。 或者如果您认为内置的拼音词库占用内存较多,也可以使用自定义词库。

自定义词库非常简单。

首先,在 lv_conf.h 中将 LV_IME_PINYIN_USE_DEFAULT_DICT 设置为 0

然后,按照以下格式编写词库。

English

If you don't want to use the built-in Pinyin dictionary, you can use the custom dictionary. Or if you think that the built-in phonetic dictionary consumes a lot of memory, you can also use a custom dictionary.

Customizing the dictionary is very simple.

First, set LV_IME_PINYIN_USE_DEFAULT_DICT to 0 in lv_conf.h

Then, write a dictionary in the following format.

词库格式

各个拼音音节的排列顺序非常重要。您需要按照汉语拼音音节表定制自己的词库,可以阅读这里了解汉语拼音音节以及音节表。

然后,根据以下格式编写自己的词库:

English

The arrangement order of each pinyin syllable is very important. You need to customize your own thesaurus according to the Hanyu Pinyin syllable table. You can read here to learn about the Hanyu Pinyin syllables and the syllable table.

Then, write your own dictionary according to the following format:

lv_100ask_pinyin_dict_t your_pinyin_dict[] = {
            { "a", "啊阿呵吖" },
            { "ai", "埃挨哎唉哀皑蔼矮碍爱隘癌艾" },
            { "an", "按安暗岸俺案鞍氨胺厂广庵揞犴铵桉谙鹌埯黯" },
            { "ang", "昂肮盎仰" },
            { "ao", "凹敖熬翱袄傲奥懊澳" },
            { "ba", "芭捌叭吧笆八疤巴拔跋靶把坝霸罢爸扒耙" },
            { "bai", "白摆佰败拜柏百稗伯" },
            /* ...... */
            { "zuo", "昨左佐做作坐座撮琢柞"},
            {NULL, NULL}

最后一项 必须以 {null, null} 结束,否则无法正常工作。

应用新词库

按照上述词库格式编写好自己的词库后,只需调用以下函数即可设置和使用您的词库:

English

After writing a dictionary according to the above dictionary format, you only need to call this function to set up and use your dictionary:

    lv_obj_t * pinyin_ime = lv_100ask_pinyin_ime_create(lv_scr_act());
    lv_100ask_pinyin_ime_set_dict(pinyin_ime, your_pinyin_dict);

输入模式

lv_ime_pinyin 支持 26 键和 9 键输入模式。模式切换非常简单,只需调用函数 lv_ime_pinyin_set_mode。如果函数 lv_ime_pinyin_set_mode 的第二个参数为 1,则切换到 26 键输入模式;如果为 0,则切换到 9 键输入模式,默认值为 1

English

lv_ime_pinyin supports 26 key and 9 key input modes. The mode switching is very simple, just call the function lv_ime_pinyin_set_mode. If the second parameter of function lv_ime_pinyin_set_mode is' 1 ', switch to 26 key input mode; if it is' 0', switch to 9 key input mode, and the default is' 1 '.

示例

Pinyin IME 26 key input

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_LABEL && LV_USE_TEXTAREA && LV_FONT_SIMSUN_16_CJK && LV_USE_IME_PINYIN && LV_BUILD_EXAMPLES

static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    lv_obj_t * kb = lv_event_get_user_data(e);

    if(code == LV_EVENT_FOCUSED) {
        if(lv_indev_get_type(lv_indev_get_act()) != LV_INDEV_TYPE_KEYPAD) {
            lv_keyboard_set_textarea(kb, ta);
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
        }
    }
    else if(code == LV_EVENT_CANCEL) {
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_state(ta, LV_STATE_FOCUSED);
        lv_indev_reset(NULL, ta);   /*To forget the last clicked object to make it focusable again*/
    }
}

void lv_example_ime_pinyin_1(void)
{
    lv_obj_t * pinyin_ime = lv_ime_pinyin_create(lv_scr_act());
    lv_obj_set_style_text_font(pinyin_ime, &lv_font_simsun_16_cjk, 0);
    //lv_ime_pinyin_set_dict(pinyin_ime, your_dict); // Use a custom dictionary. If it is not set, the built-in dictionary will be used.

    /* ta1 */
    lv_obj_t * ta1 = lv_textarea_create(lv_scr_act());
    lv_textarea_set_one_line(ta1, true);
    lv_obj_set_style_text_font(ta1, &lv_font_simsun_16_cjk, 0);
    lv_obj_align(ta1, LV_ALIGN_TOP_LEFT, 0, 0);

    /*Create a keyboard and add it to ime_pinyin*/
    lv_obj_t * kb = lv_keyboard_create(lv_scr_act());
    lv_ime_pinyin_set_keyboard(pinyin_ime, kb);
    lv_keyboard_set_textarea(kb, ta1);

    lv_obj_add_event_cb(ta1, ta_event_cb, LV_EVENT_ALL, kb);

    /*Get the cand_panel, and adjust its size and position*/
    lv_obj_t * cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
    lv_obj_set_size(cand_panel, LV_PCT(100), LV_PCT(10));
    lv_obj_align_to(cand_panel, kb, LV_ALIGN_OUT_TOP_MID, 0, 0);

    /*Try using ime_pinyin to output the Chinese below in the ta1 above*/
    lv_obj_t * cz_label = lv_label_create(lv_scr_act());
    lv_label_set_text(cz_label,
                      "嵌入式系统(Embedded System),\n是一种嵌入机械或电气系统内部、具有专一功能和实时计算性能的计算机系统。");
    lv_obj_set_style_text_font(cz_label, &lv_font_simsun_16_cjk, 0);
    lv_obj_set_width(cz_label, 310);
    lv_obj_align_to(cz_label, ta1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
}

#endif

MicroPython code  

 GitHub Simulator
Error encountered while trying to open D:\lv_port_pc_eclipse-release-v8.3\lvgl\examples\others\ime\lv_example_ime_pinyin_1.py

Pinyin IME 9 key input

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_LABEL && LV_USE_TEXTAREA && LV_FONT_SIMSUN_16_CJK && LV_USE_IME_PINYIN && LV_IME_PINYIN_USE_K9_MODE && LV_BUILD_EXAMPLES

static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    lv_obj_t * kb = lv_event_get_user_data(e);

    if(code == LV_EVENT_FOCUSED) {
        if(lv_indev_get_type(lv_indev_get_act()) != LV_INDEV_TYPE_KEYPAD) {
            lv_keyboard_set_textarea(kb, ta);
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
        }
    }
    else if(code == LV_EVENT_READY) {
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_state(ta, LV_STATE_FOCUSED);
        lv_indev_reset(NULL, ta);   /*To forget the last clicked object to make it focusable again*/
    }
}

void lv_example_ime_pinyin_2(void)
{
    lv_obj_t * pinyin_ime = lv_ime_pinyin_create(lv_scr_act());
    lv_obj_set_style_text_font(pinyin_ime, &lv_font_simsun_16_cjk, 0);
    //lv_ime_pinyin_set_dict(pinyin_ime, your_dict); // Use a custom dictionary. If it is not set, the built-in dictionary will be used.

    /* ta1 */
    lv_obj_t * ta1 = lv_textarea_create(lv_scr_act());
    lv_textarea_set_one_line(ta1, true);
    lv_obj_set_style_text_font(ta1, &lv_font_simsun_16_cjk, 0);
    lv_obj_align(ta1, LV_ALIGN_TOP_LEFT, 0, 0);

    /*Create a keyboard and add it to ime_pinyin*/
    lv_obj_t * kb = lv_keyboard_create(lv_scr_act());
    lv_keyboard_set_textarea(kb, ta1);

    lv_ime_pinyin_set_keyboard(pinyin_ime, kb);
    lv_ime_pinyin_set_mode(pinyin_ime,
                           LV_IME_PINYIN_MODE_K9);  // Set to 9-key input mode. Default: 26-key input(k26) mode.
    lv_obj_add_event_cb(ta1, ta_event_cb, LV_EVENT_ALL, kb);

    /*Get the cand_panel, and adjust its size and position*/
    lv_obj_t * cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
    lv_obj_set_size(cand_panel, LV_PCT(100), LV_PCT(10));
    lv_obj_align_to(cand_panel, kb, LV_ALIGN_OUT_TOP_MID, 0, 0);

    /*Try using ime_pinyin to output the Chinese below in the ta1 above*/
    lv_obj_t * cz_label = lv_label_create(lv_scr_act());
    lv_label_set_text(cz_label,
                      "嵌入式系统(Embedded System),\n是一种嵌入机械或电气系统内部、具有专一功能和实时计算性能的计算机系统。");
    lv_obj_set_style_text_font(cz_label, &lv_font_simsun_16_cjk, 0);
    lv_obj_set_width(cz_label, 310);
    lv_obj_align_to(cz_label, ta1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
}

#endif

MicroPython code  

 GitHub Simulator
Error encountered while trying to open D:\lv_port_pc_eclipse-release-v8.3\lvgl\examples\others\ime\lv_example_ime_pinyin_2.py

API

Enums

enum lv_ime_pinyin_mode_t

Values:

enumerator LV_IME_PINYIN_MODE_K26
enumerator LV_IME_PINYIN_MODE_K9

Functions

lv_obj_t *lv_ime_pinyin_create(lv_obj_t *parent)
void lv_ime_pinyin_set_keyboard(lv_obj_t *obj, lv_obj_t *kb)

Set the keyboard of Pinyin input method.

Parameters
  • obj -- pointer to a Pinyin input method object

  • dict -- pointer to a Pinyin input method keyboard

void lv_ime_pinyin_set_dict(lv_obj_t *obj, lv_pinyin_dict_t *dict)

Set the dictionary of Pinyin input method.

Parameters
  • obj -- pointer to a Pinyin input method object

  • dict -- pointer to a Pinyin input method dictionary

void lv_ime_pinyin_set_mode(lv_obj_t *obj, lv_ime_pinyin_mode_t mode)

Set mode, 26-key input(k26) or 9-key input(k9).

Parameters
  • obj -- pointer to a Pinyin input method object

  • mode -- the mode from 'lv_ime_pinyin_mode_t'

lv_obj_t *lv_ime_pinyin_get_kb(lv_obj_t *obj)

Set the dictionary of Pinyin input method.

Parameters

obj -- pointer to a Pinyin IME object

Returns

pointer to the Pinyin IME keyboard

lv_obj_t *lv_ime_pinyin_get_cand_panel(lv_obj_t *obj)

Set the dictionary of Pinyin input method.

Parameters

obj -- pointer to a Pinyin input method object

Returns

pointer to the Pinyin input method candidate panel

lv_pinyin_dict_t *lv_ime_pinyin_get_dict(lv_obj_t *obj)

Set the dictionary of Pinyin input method.

Parameters

obj -- pointer to a Pinyin input method object

Returns

pointer to the Pinyin input method dictionary

struct lv_pinyin_dict_t

Public Members

const char *const py
const char *const py_mb
struct ime_pinyin_k9_py_str_t

Public Members

char py_str[7]
struct lv_ime_pinyin_t

Public Members

lv_obj_t obj
lv_obj_t *kb
lv_obj_t *cand_panel
lv_pinyin_dict_t *dict
char *cand_str
char input_char[16]
char k9_input_str[LV_IME_PINYIN_K9_MAX_INPUT]
uint16_t k9_py_ll_pos
uint16_t k9_input_str_len
uint16_t ta_count
uint16_t cand_num
uint16_t py_page
uint16_t py_num[26]
uint16_t py_pos[26]
uint8_t mode