输入法服务

说明:

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import inputMethodEngine from '@ohos.inputmethodengine';

inputMethodEngine

常量值。

系统能力:SystemCapability.Miscservices.InputMethodFramework

名称 参数类型 可读 可写 说明
ENTER_KEY_TYPE_UNSPECIFIED number 无功能键。
ENTER_KEY_TYPE_GO number “前往”功能键。
ENTER_KEY_TYPE_SEARCH number “搜索”功能键。
ENTER_KEY_TYPE_SEND number “发送”功能键。
ENTER_KEY_TYPE_NEXT number “下一个”功能键。
ENTER_KEY_TYPE_DONE number “回车”功能键。
ENTER_KEY_TYPE_PREVIOUS number “前一个”功能键。
PATTERN_NULL number 无特殊性编辑框。
PATTERN_TEXT number 文本编辑框。
PATTERN_NUMBER number 数字编辑框。
PATTERN_PHONE number 电话号码编辑框。
PATTERN_DATETIME number 日期编辑框。
PATTERN_EMAIL number 邮件编辑框。
PATTERN_URI number 超链接编辑框。
PATTERN_PASSWORD number 密码编辑框。
OPTION_ASCII number 允许输入ASCII值。
OPTION_NONE number 不指定编辑框输入属性。
OPTION_AUTO_CAP_CHARACTERS number 允许输入字符。
OPTION_AUTO_CAP_SENTENCES number 允许输入句子。
OPTION_AUTO_WORDS number 允许输入单词。
OPTION_MULTI_LINE number 允许输入多行。
OPTION_NO_FULLSCREEN number 半屏样式。
FLAG_SELECTING number 编辑框处于选择状态。
FLAG_SINGLE_LINE number 编辑框为单行。
DISPLAY_MODE_PART number 编辑框显示为半屏。
DISPLAY_MODE_FULL number 编辑框显示为全屏。

inputMethodEngine.getInputMethodEngine

getInputMethodEngine(): InputMethodEngine

获取服务端实例。

系统能力: SystemCapability.Miscservices.InputMethodFramework

返回值:

类型 说明
InputMethodEngine 服务端实例。

示例:

let InputMethodEngine = inputMethodEngine.getInputMethodEngine();

inputMethodEngine.createKeyboardDelegate

createKeyboardDelegate(): KeyboardDelegate

获取客户端监听实例。

系统能力: SystemCapability.Miscservices.InputMethodFramework

返回值:

类型 说明
KeyboardDelegate 客户端监听实例。

示例:

let KeyboardDelegate = inputMethodEngine.createKeyboardDelegate();

InputMethodEngine

下列API示例中都需使用getInputMethodEngine回调获取到InputMethodEngine实例,再通过此实例调用对应方法。

on('inputStart')

on(type: 'inputStart', callback: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void

订阅输入法绑定成功事件,使用callback回调返回输入法操作相关实例。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型。
-type为‘inputStart’时表示订阅输入法绑定。
callback KeyboardController, TextInputClient 回调返回输入法操作相关实例。

示例:

InputMethodEngine.on('inputStart', (kbController, textInputClient) => {
    KeyboardController = kbController;
    TextInputClient = textInputClient;
});

off('inputStart')

off(type: 'inputStart', callback?: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void

取消订阅输入法绑定成功事件。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型。
-type为‘inputStart’时表示订阅输入法绑定。
callback KeyboardController, TextInputClient 回调返回输入法操作相关实例。

示例:

InputMethodEngine.off('inputStart');

on('keyboardShow'|'keyboardHide')

on(type: 'keyboardShow'|'keyboardHide', callback: () => void): void

订阅输入法事件。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型。
- type为'keyboardShow',表示订阅输入法显示。
- type为'keyboardHide',表示订阅输入法隐藏。
callback void 回调函数。

示例:

InputMethodEngine.on('keyboardShow', (err) => {
    console.info('keyboardShow');
});

off('keyboardShow'|'keyboardHide')

off(type: 'keyboardShow'|'keyboardHide', callback?: () => void): void

取消订阅输入法事件。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型。
- type为'keyboardShow',表示订阅输入法显示。
- type为'keyboardHide',表示订阅输入法隐藏。
callback void 回调函数。

示例:

InputMethodEngine.off('keyboardShow');

KeyboardDelegate

下列API示例中都需使用createKeyboardDelegate回调获取到KeyboardDelegate实例,再通过此实例调用对应方法。

on('keyDown'|'keyUp')

on(type: 'keyDown'|'keyUp', callback: (event: KeyEvent) => boolean): void

订阅硬键盘事件,使用callback回调返回按键信息。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型。
- type为'keyDown',表示订阅硬键盘按下。
- type为'keyUp',表示订阅硬键盘抬起。
callback KeyEvent 回调返回按键信息。

示例:

KeyboardDelegate.on('keyDown', (event) => {
    console.info('keyDown');
});

off('keyDown'|'keyUp')

off(type: 'keyDown'|'keyUp', callback?: (event: KeyEvent) => boolean): void

取消订阅硬键盘事件。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型。
- type为'keyDown',表示订阅硬键盘按下。
- type为'keyUp',表示订阅硬键盘抬起。
callback KeyEvent 回调返回按键信息。

示例:

KeyboardDelegate.off('keyDown');

on('cursorContextChange')

on(type: 'cursorContextChange', callback: (x: number, y:number, height:number) => void): void

订阅光标变化事件,使用callback回调返回光标信息。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 光标变化事件。
-type为’cursorContextChange‘时,表示光标变化。
callback number 回调返回光标信息。

示例:

KeyboardDelegate.on('cursorContextChange', (x, y, height) => {
    console.info('cursorContextChange');
});

off('cursorContextChange')

off(type: 'cursorContextChange', callback?: (x: number, y:number, height:number) => void): void

取消订阅光标变化事件。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 光标变化事件。
-type为’cursorContextChange‘时,表示光标变化。
callback number 回调返回光标信息。

示例:

KeyboardDelegate.off('cursorContextChange');

on('selectionChange')

on(type: 'selectionChange', callback: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void

订阅文本选择变化事件,使用callback回调返回文本选择信息。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 文本选择变化事件。
-type为’selectionChange‘时,表示选择文本变化。
callback number 回调返回文本选择信息。

示例:

KeyboardDelegate.on('selectionChange', (oldBegin, oldEnd, newBegin, newEnd) => {
    console.info('selectionChange');
});

off('selectionChange')

off(type: 'selectionChange', callback?: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void

取消订阅文本选择变化事件。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 文本选择变化事件。
-type为’selectionChange‘时,表示选择文本变化。
callback number 回调返回文本选择信息。

示例:

KeyboardDelegate.off('selectionChange');

on('textChange')

on(type: 'textChange', callback: (text: string) => void): void

订阅文本变化事件,使用callback回调返回当前文本内容。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 文本变化事件。
-type为’textChange‘时,表示当前文本变化。
callback string 回调返回当前文本内容。

示例:

KeyboardDelegate.on('textChange', (text) => {
    console.info('textChange');
});

off('textChange')

off(type: 'textChange', callback?: (text: string) => void): void

取消订阅文本变化事件。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 文本变化事件。
-type为’textChange‘时,表示当前文本变化。
callback string 回调返回当前文本内容。

示例:

KeyboardDelegate.off('textChange');

KeyboardController

下列API示例中都需使用inputStart回调获取到KeyboardController实例,再通过此实例调用对应方法。

hideKeyboard

hideKeyboard(callback: AsyncCallback<void>): void

隐藏输入法。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数

示例:

KeyboardController.hideKeyboard(()=>{
});

hideKeyboard

hideKeyboard(): Promise<void>

隐藏输入法。

系统能力: SystemCapability.Miscservices.InputMethodFramework

返回值:

类型 说明
Promise<void> 回调函数

示例:

KeyboardController.hideKeyboard();

TextInputClient

下列API示例中都需使用inputStart回调获取到TextInputClient实例,再通过此实例调用对应方法。

getForward

getForward(length:number, callback: AsyncCallback<string>): void

获取光标前固定长度的文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
length number 文本长度。
callback AsyncCallback<string> 返回文本。

示例:

TextInputClient.getForward(5,(text) =>{
    console.info("text = " + text);
});

getForward

getForward(length:number): Promise<string>

获取光标前固定长度的文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
length number 文本长度。

返回值:

类型 说明
Promise<string> 返回文本。

示例:

let text = TextInputClient.getForward(5);
console.info("text = " + text);

getBackward

getBackward(length:number, callback: AsyncCallback<string>): void

获取光标后固定长度的文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
length number 文本长度。
callback AsyncCallback<string> 返回文本。

示例:

TextInputClient.getBackward(5,(text)=>{
    console.info("text = " + text);
});

getBackward

getBackward(length:number): Promise<string>

获取光标后固定长度的文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
length number 文本长度。

返回值:

类型 说明
Promise<string> 返回文本。

示例:

let text = TextInputClient.getBackward(5);
console.info("text = " + text);

deleteForward

deleteForward(length:number, callback: AsyncCallback<boolean>): void

删除光标前固定长度的文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
length number 文本长度。
callback AsyncCallback<boolean> 操作成功与否。

示例:

TextInputClient.deleteForward(5,(isSuccess)=>{
    console.info("isSuccess = " + isSuccess);
});

deleteForward

deleteForward(length:number): Promise<boolean>

删除光标前固定长度的文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
length number 文本长度。

返回值:

类型 说明
Promise<boolean> 操作成功与否。

示例:

let isSuccess = TextInputClient.deleteForward(5);
console.info("isSuccess = " + isSuccess);

deleteBackward

deleteBackward(length:number, callback: AsyncCallback<boolean>): void

删除光标后固定长度的文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
length number 文本长度。
callback AsyncCallback<boolean> 操作成功与否。

示例:

TextInputClient.deleteBackward(5, (isSuccess)=>{
    console.info("isSuccess = " + isSuccess);
});

deleteBackward

deleteBackward(length:number): Promise<boolean>

删除光标后固定长度的文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
length number 文本长度。

返回值:

类型 说明
Promise<boolean> 操作成功与否。

示例:

let isSuccess = TextInputClient.deleteBackward(5);
console.info("isSuccess = " + isSuccess);

sendKeyFunction

sendKeyFunction(action:number, callback: AsyncCallback<boolean>): void

发送功能键。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
action number 编辑框属性。
callback AsyncCallback<boolean> 操作成功与否。

示例:

TextInputClient.sendKeyFunction(inputMethod.ENTER_KEY_TYPE_NEXT,(isSuccess)=>{
    console.info("isSuccess = " + isSuccess);
});

sendKeyFunction

sendKeyFunction(action:number): Promise<boolean>

发送功能键。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
action number 编辑框属性。

返回值:

类型 说明
Promise<boolean> 操作成功与否。

示例:

let isSuccess = TextInputClient.sendKeyFunction(inputMethod.ENTER_KEY_TYPE_NEXT);
console.info("isSuccess = " + isSuccess);

insertText

insertText(text:string, callback: AsyncCallback<boolean>): void

插入文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
text string 文本。
callback AsyncCallback<boolean> 操作成功与否。

示例:

TextInputClient.insertText("test", (isSuccess)=>{
    console.info("isSuccess = " + isSuccess);
});

insertText

insertText(text:string): Promise<boolean>

插入文本。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
text string 文本。

返回值:

类型 说明
Promise<boolean> 操作成功与否。

示例:

let isSuccess = TextInputClient.insertText("test");
console.info("isSuccess = " + isSuccess);

getEditorAttribute

getEditorAttribute(callback: AsyncCallback<EditorAttribute>): void

获取编辑框属性值。

系统能力: SystemCapability.Miscservices.InputMethodFramework

参数:

参数名 类型 必填 说明
callback AsyncCallback<EditorAttribute> 编辑框属性值。

示例:

TextInputClient.getEditorAttribute((EditorAttribute)=>{
});

getEditorAttribute

getEditorAttribute(): Promise<EditorAttribute>

获取编辑框属性值。

系统能力: SystemCapability.Miscservices.InputMethodFramework

返回值:

类型 说明
Promise<EditorAttribute> 返回编辑框属性值。

示例:

let EditorAttribute = TextInputClient.getEditorAttribute();

EditorAttribute

编辑框属性值。

系统能力: SystemCapability.Miscservices.InputMethodFramework

名称 参数类型 可读 可写 说明
enterKeyType number 编辑框的功能属性。
inputPattern number 编辑框的文本属性。

KeyEvent

按键属性值。

系统能力: SystemCapability.Miscservices.InputMethodFramework

名称 参数类型 可读 可写 说明
keyCode number 按键的键值。
keyAction number 按键的状态。