【鸿蒙next开发】输入法开发:@ohos.inputMethod (输入法框架)_ohos中文输入法
往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)
-
鸿蒙开发核心知识点,看这篇文章就够了
-
最新版!鸿蒙HarmonyOS Next应用开发实战学习路线
-
鸿蒙HarmonyOS NEXT开发技术最全学习路线指南
-
鸿蒙应用开发实战项目,看这一篇文章就够了(部分项目附源码)
@ohos.inputMethod (输入法框架)
本模块主要面向普通前台应用(备忘录、信息、设置等系统应用与三方应用),提供对输入法(输入法应用)的控制、管理能力,包括显示/隐藏输入法软键盘、切换输入法、获取所有输入法列表等等。
说明
本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
导入模块
import { inputMethod } from \'@kit.IMEKit\';
常量8+
常量值。
系统能力: SystemCapability.MiscServices.InputMethodFramework
InputMethodProperty8+
输入法应用属性。
系统能力: SystemCapability.MiscServices.InputMethodFramework
输入法扩展信息。预留字段,当前无具体含义,暂不支持使用。
- API version 10起:非必填;
- API version 9:必填。
输入法包名。必填。
说明:从API version 8开始支持,从API version 9开始废弃,建议使用name替代。
输入法唯一标识。必填。
说明:从API version 8开始支持,从API version 9开始废弃,建议使用id替代。
inputMethod.getController9+
getController(): InputMethodController
获取客户端实例InputMethodController。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
let inputMethodController = inputMethod.getController();
inputMethod.getDefaultInputMethod11+
getDefaultInputMethod(): InputMethodProperty
获取默认输入法。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
try { let defaultIme = inputMethod.getDefaultInputMethod();} catch(err) { console.error(`Failed to getDefaultInputMethod: ${JSON.stringify(err)}`);}
inputMethod.getSystemInputMethodConfigAbility11+
getSystemInputMethodConfigAbility(): ElementName
获取系统输入法设置界面Ability信息。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
try { let inputMethodConfig = inputMethod.getSystemInputMethodConfigAbility();} catch(err) { console.error(`Failed to get getSystemInputMethodConfigAbility: ${JSON.stringify(err)}`);}
inputMethod.getSetting9+
getSetting(): InputMethodSetting
获取客户端设置实例InputMethodSetting。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
let inputMethodSetting = inputMethod.getSetting();
inputMethod.switchInputMethod9+
switchInputMethod(target: InputMethodProperty, callback: AsyncCallback): void
切换输入法,使用callback异步回调。
说明
- 在API version 9-10版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY。
- 在API version 11版本起,仅支持当前输入法应用调用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';let currentIme = inputMethod.getCurrentInputMethod();try{ inputMethod.switchInputMethod(currentIme, (err: BusinessError, result: boolean) => { if (err) { console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`); return; } if (result) { console.log(\'Succeeded in switching inputmethod.\'); } else { console.error(\'Failed to switchInputMethod.\'); } });} catch(err) { console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`);}
说明
在 api11 中 201 permissions check fails. 这个错误码被移除。
inputMethod.switchInputMethod9+
switchInputMethod(target: InputMethodProperty): Promise
切换输入法,使用promise异步回调。
说明
- 在API version 9-10版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY。
- 在API version 11版本起,仅支持当前输入法应用调用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';let currentIme = inputMethod.getCurrentInputMethod();try { inputMethod.switchInputMethod(currentIme).then((result: boolean) => { if (result) { console.log(\'Succeeded in switching inputmethod.\'); } else { console.error(\'Failed to switchInputMethod.\'); } }).catch((err: BusinessError) => { console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`); })} catch (err) { console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`);}
说明
在 api11 中 201 permissions check fails. 这个错误码被移除。
inputMethod.getCurrentInputMethod9+
getCurrentInputMethod(): InputMethodProperty
使用同步方法获取当前输入法。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
示例:
let currentIme = inputMethod.getCurrentInputMethod();
inputMethod.switchCurrentInputMethodSubtype9+
switchCurrentInputMethodSubtype(target: InputMethodSubtype, callback: AsyncCallback): void
切换当前输入法的子类型。使用callback异步回调。
说明
- 在API version 9版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY。
- 在API version 10版本,支持系统应用和当前输入法应用调用;需要权限ohos.permission.CONNECT_IME_ABILITY。
- 在API version 11版本起,仅支持当前输入法调用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let extra: Record = {} inputMethod.switchCurrentInputMethodSubtype({ id: \"ServiceExtAbility\", label: \"\", name: \"com.example.kikakeyboard\", mode: \"upper\", locale: \"\", language: \"\", icon: \"\", iconId: 0, extra: extra }, (err: BusinessError, result: boolean) => { if (err) { console.error(`Failed to switchCurrentInputMethodSubtype: ${JSON.stringify(err)}`); return; } if (result) { console.log(\'Succeeded in switching currentInputMethodSubtype.\'); } else { console.error(\'Failed to switchCurrentInputMethodSubtype\'); } });} catch(err) { console.error(`Failed to switchCurrentInputMethodSubtype: ${JSON.stringify(err)}`);}
说明
在 api11 中 201 permissions check fails. 这个错误码被移除。
inputMethod.switchCurrentInputMethodSubtype9+
switchCurrentInputMethodSubtype(target: InputMethodSubtype): Promise
切换当前输入法的子类型。使用promise异步回调。
说明
- 在API version 9版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY。
- 在API version 10版本,支持系统应用和当前输入法应用调用;需要权限ohos.permission.CONNECT_IME_ABILITY。
- 在API version 11版本起,仅支持当前输入法调用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let extra: Record = {} inputMethod.switchCurrentInputMethodSubtype({ id: \"ServiceExtAbility\", label: \"\", name: \"com.example.kikakeyboard\", mode: \"upper\", locale: \"\", language: \"\", icon: \"\", iconId: 0, extra: extra }).then((result: boolean) => { if (result) { console.log(\'Succeeded in switching currentInputMethodSubtype.\'); } else { console.error(\'Failed to switchCurrentInputMethodSubtype.\'); } }).catch((err: BusinessError) => { console.error(`Failed to switchCurrentInputMethodSubtype: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to switchCurrentInputMethodSubtype: ${JSON.stringify(err)}`);}
说明
在 api11 中 201 permissions check fails. 这个错误码被移除。
inputMethod.getCurrentInputMethodSubtype9+
getCurrentInputMethodSubtype(): InputMethodSubtype
获取当前输入法的子类型。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
示例:
let currentImeSubType = inputMethod.getCurrentInputMethodSubtype();
inputMethod.switchCurrentInputMethodAndSubtype9+
switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype, callback: AsyncCallback): void
切换至指定输入法的指定子类型,适用于跨输入法切换子类型。使用callback异步回调。
说明
- 在API version 9-10版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY。
- 在API version 11版本起,仅支持当前输入法调用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';let currentIme = inputMethod.getCurrentInputMethod();let imSubType = inputMethod.getCurrentInputMethodSubtype();try { inputMethod.switchCurrentInputMethodAndSubtype(currentIme, imSubType, (err: BusinessError, result: boolean) => { if (err) { console.error(`Failed to switchCurrentInputMethodAndSubtype: ${JSON.stringify(err)}`); return; } if (result) { console.log(\'Succeeded in switching currentInputMethodAndSubtype.\'); } else { console.error(\'Failed to switchCurrentInputMethodAndSubtype.\'); } });} catch (err) { console.error(`Failed to switchCurrentInputMethodAndSubtype: ${JSON.stringify(err)}`);}
说明
在 api11 中 201 permissions check fails. 这个错误码被移除。
inputMethod.switchCurrentInputMethodAndSubtype9+
switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype): Promise
切换至指定输入法的指定子类型,适用于跨输入法切换子类型。使用promise异步回调。
说明
- 在API version 9-10版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY。
- 在API version 11版本起,仅支持当前输入法调用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';let currentIme = inputMethod.getCurrentInputMethod();let imSubType = inputMethod.getCurrentInputMethodSubtype();try { inputMethod.switchCurrentInputMethodAndSubtype(currentIme, imSubType).then((result: boolean) => { if (result) { console.log(\'Succeeded in switching currentInputMethodAndSubtype.\'); } else { console.error(\'Failed to switchCurrentInputMethodAndSubtype.\'); } }).catch((err: BusinessError) => { console.error(`Failed to switchCurrentInputMethodAndSubtype: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to switchCurrentInputMethodAndSubtype: ${JSON.stringify(err)}`);}
说明
在 api11 中 201 permissions check fails. 这个错误码被移除。
inputMethod.getInputMethodController(deprecated)
getInputMethodController(): InputMethodController
获取客户端实例InputMethodController。
说明
从API version 6开始支持,从API version 9开始废弃,建议使用getController()替代。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
示例:
let inputMethodController = inputMethod.getInputMethodController();
inputMethod.getInputMethodSetting(deprecated)
getInputMethodSetting(): InputMethodSetting
获取客户端设置实例InputMethodSetting。
说明
从API version 6开始支持,从API version 9开始废弃,建议使用getSetting()替代。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
示例:
let inputMethodSetting = inputMethod.getInputMethodSetting();
TextInputType10+
文本输入类型。
系统能力: SystemCapability.MiscServices.InputMethodFramework
EnterKeyType10+
Enter键的功能类型。
系统能力: SystemCapability.MiscServices.InputMethodFramework
KeyboardStatus10+
输入法软键盘状态。
系统能力: SystemCapability.MiscServices.InputMethodFramework
Direction10+
光标移动方向。
系统能力: SystemCapability.MiscServices.InputMethodFramework
ExtendAction10+
编辑框中文本的扩展编辑操作类型,如剪切、复制等。
系统能力: SystemCapability.MiscServices.InputMethodFramework
FunctionKey10+
输入法功能键类型。
系统能力: SystemCapability.MiscServices.InputMethodFramework
InputAttribute10+
编辑框属性,包含文本输入类型和Enter键功能类型。
系统能力: SystemCapability.MiscServices.InputMethodFramework
TextConfig10+
编辑框的配置信息。
系统能力: SystemCapability.MiscServices.InputMethodFramework
CursorInfo10+
光标信息。
系统能力: SystemCapability.MiscServices.InputMethodFramework
Range10+
文本的选中范围。
系统能力: SystemCapability.MiscServices.InputMethodFramework
Movement10+
选中文本时,光标移动的方向。
系统能力: SystemCapability.MiscServices.InputMethodFramework
InputWindowInfo10+
输入法软键盘的窗口信息。
系统能力: SystemCapability.MiscServices.InputMethodFramework
InputMethodController
下列API示例中都需使用getController获取到InputMethodController实例,再通过实例调用对应方法。
attach10+
attach(showKeyboard: boolean, textConfig: TextConfig, callback: AsyncCallback): void
自绘控件绑定输入法。使用callback异步回调。
说明
需要先调用此接口,完成自绘控件与输入法的绑定,才能使用以下功能:显示/隐藏键盘、更新光标信息、更改编辑框选中范围、保存配置信息、监听处理由输入法应用发送的信息或命令等。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
绑定输入法成功后,是否拉起输入法键盘。
- true表示拉起,false表示不拉起。
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let textConfig: inputMethod.TextConfig = { inputAttribute: { textInputType: 0, enterKeyType: 1 } }; inputMethodController.attach(true, textConfig, (err: BusinessError) => { if (err) { console.error(`Failed to attach: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in attaching the inputMethod.\'); });} catch(err) { console.error(`Failed to attach: ${JSON.stringify(err)}`);}
attach10+
attach(showKeyboard: boolean, textConfig: TextConfig): Promise
自绘控件绑定输入法。使用promise异步回调。
说明
需要先调用此接口,完成自绘控件与输入法的绑定,才能使用以下功能:显示/隐藏键盘、更新光标信息、更改编辑框选中范围、保存配置信息、监听处理由输入法应用发送的信息或命令等。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
绑定输入法成功后,是否拉起输入法键盘。
- true表示拉起,false表示不拉起。
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let textConfig: inputMethod.TextConfig = { inputAttribute: { textInputType: 0, enterKeyType: 1 } }; inputMethodController.attach(true, textConfig).then(() => { console.log(\'Succeeded in attaching inputMethod.\'); }).catch((err: BusinessError) => { console.error(`Failed to attach: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to attach: ${JSON.stringify(err)}`);}
showTextInput10+
showTextInput(callback: AsyncCallback): void
进入文本编辑状态。使用callback异步回调。
说明
编辑框与输入法绑定成功后,可调用该接口拉起软键盘,进入文本编辑状态。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.showTextInput((err: BusinessError) => { if (err) { console.error(`Failed to showTextInput: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in showing the inputMethod.\');});
showTextInput10+
showTextInput(): Promise
进入文本编辑状态。使用promise异步回调。
说明
编辑框与输入法绑定成功后,可调用该接口拉起软键盘,进入文本编辑状态。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.showTextInput().then(() => { console.log(\'Succeeded in showing text input.\');}).catch((err: BusinessError) => { console.error(`Failed to showTextInput: ${JSON.stringify(err)}`);});
hideTextInput10+
hideTextInput(callback: AsyncCallback): void
退出文本编辑状态。使用callback异步回调。
说明
调用接口时,若软键盘处于显示状态,调用接口后软键盘会被隐藏。
调用该接口不会解除与输入法的绑定,再次调用showTextInput时,可重新进入文本编辑状态。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.hideTextInput((err: BusinessError) => { if (err) { console.error(`Failed to hideTextInput: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in hiding text input.\');});
hideTextInput10+
hideTextInput(): Promise
退出文本编辑状态。使用promise异步回调。
说明
调用接口时,若软键盘处于显示状态,调用接口后软键盘会被隐藏。
调用该接口不会解除与输入法的绑定,再次调用showTextInput时,可重新进入文本编辑状态。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.hideTextInput().then(() => { console.log(\'Succeeded in hiding inputMethod.\');}).catch((err: BusinessError) => { console.error(`Failed to hideTextInput: ${JSON.stringify(err)}`);})
detach10+
detach(callback: AsyncCallback): void
自绘控件解除与输入法的绑定。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.detach((err: BusinessError) => { if (err) { console.error(`Failed to detach: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in detaching inputMethod.\');});
detach10+
detach(): Promise
自绘控件解除与输入法的绑定。使用promise异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.detach().then(() => { console.log(\'Succeeded in detaching inputMethod.\');}).catch((err: BusinessError) => { console.error(`Failed to detach: ${JSON.stringify(err)}`);});
setCallingWindow10+
setCallingWindow(windowId: number, callback: AsyncCallback): void
设置要避让软键盘的窗口。使用callback异步回调。
说明
将绑定到输入法的应用程序所在的窗口Id传入,此窗口可以避让输入法窗口。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let windowId: number = 2000; inputMethodController.setCallingWindow(windowId, (err: BusinessError) => { if (err) { console.error(`Failed to setCallingWindow: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in setting callingWindow.\'); });} catch(err) { console.error(`Failed to setCallingWindow: ${JSON.stringify(err)}`);}
setCallingWindow10+
setCallingWindow(windowId: number): Promise
设置要避让软键盘的窗口。使用promise异步回调。
说明
将绑定到输入法的应用程序所在的窗口Id传入,此窗口可以避让输入法窗口。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let windowId: number = 2000; inputMethodController.setCallingWindow(windowId).then(() => { console.log(\'Succeeded in setting callingWindow.\'); }).catch((err: BusinessError) => { console.error(`Failed to setCallingWindow: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to setCallingWindow: ${JSON.stringify(err)}`);}
updateCursor10+
updateCursor(cursorInfo: CursorInfo, callback: AsyncCallback): void
当编辑框内的光标信息发生变化时,调用该接口使输入法感知到光标变化。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let cursorInfo: inputMethod.CursorInfo = { left: 0, top: 0, width: 600, height: 800 }; inputMethodController.updateCursor(cursorInfo, (err: BusinessError) => { if (err) { console.error(`Failed to updateCursor: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in updating cursorInfo.\'); });} catch(err) { console.error(`Failed to updateCursor: ${JSON.stringify(err)}`);}
updateCursor10+
updateCursor(cursorInfo: CursorInfo): Promise
当编辑框内的光标信息发生变化时,调用该接口使输入法感知到光标变化。使用promise异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let cursorInfo: inputMethod.CursorInfo = { left: 0, top: 0, width: 600, height: 800 }; inputMethodController.updateCursor(cursorInfo).then(() => { console.log(\'Succeeded in updating cursorInfo.\'); }).catch((err: BusinessError) => { console.error(`Failed to updateCursor: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to updateCursor: ${JSON.stringify(err)}`);}
changeSelection10+
changeSelection(text: string, start: number, end: number, callback: AsyncCallback): void
当编辑框内被选中的文本信息内容或文本范围发生变化时,可调用该接口更新文本信息,使输入法应用感知到变化。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { inputMethodController.changeSelection(\'text\', 0, 5, (err: BusinessError) => { if (err) { console.error(`Failed to changeSelection: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in changing selection.\'); });} catch(err) { console.error(`Failed to changeSelection: ${JSON.stringify(err)}`);}
changeSelection10+
changeSelection(text: string, start: number, end: number): Promise
当编辑框内被选中的文本信息内容或文本范围发生变化时,可调用该接口更新文本信息,使输入法应用感知到变化。使用promise异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { inputMethodController.changeSelection(\'test\', 0, 5).then(() => { console.log(\'Succeeded in changing selection.\'); }).catch((err: BusinessError) => { console.error(`Failed to changeSelection: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to changeSelection: ${JSON.stringify(err)}`);}
updateAttribute10+
updateAttribute(attribute: InputAttribute, callback: AsyncCallback): void
更新编辑框属性信息。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let inputAttribute: inputMethod.InputAttribute = { textInputType: 0, enterKeyType: 1 }; inputMethodController.updateAttribute(inputAttribute, (err: BusinessError) => { if (err) { console.error(`Failed to updateAttribute: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in updating attribute.\'); });} catch(err) { console.error(`Failed to updateAttribute: ${JSON.stringify(err)}`);}
updateAttribute10+
updateAttribute(attribute: InputAttribute): Promise
更新编辑框属性信息。使用promise异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { let inputAttribute: inputMethod.InputAttribute = { textInputType: 0, enterKeyType: 1 }; inputMethodController.updateAttribute(inputAttribute).then(() => { console.log(\'Succeeded in updating attribute.\'); }).catch((err: BusinessError) => { console.error(`Failed to updateAttribute: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to updateAttribute: ${JSON.stringify(err)}`);}
stopInputSession9+
stopInputSession(callback: AsyncCallback): void
结束输入会话。使用callback异步回调。
说明
该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { inputMethodController.stopInputSession((err: BusinessError, result: boolean) => { if (err) { console.error(`Failed to stopInputSession: ${JSON.stringify(err)}`); return; } if (result) { console.log(\'Succeeded in stopping inputSession.\'); } else { console.error(\'Failed to stopInputSession.\'); } });} catch(err) { console.error(`Failed to stopInputSession: ${JSON.stringify(err)}`);}
stopInputSession9+
stopInputSession(): Promise
结束输入会话。使用promise异步回调。
说明
该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { inputMethodController.stopInputSession((err: BusinessError, result: boolean) => { if (err) { console.error(`Failed to stopInputSession: ${JSON.stringify(err)}`); return; } if (result) { console.log(\'Succeeded in stopping inputSession.\'); } else { console.error(\'Failed to stopInputSession.\'); } });} catch(err) { console.error(`Failed to stopInputSession: ${JSON.stringify(err)}`);}
showSoftKeyboard9+
showSoftKeyboard(callback: AsyncCallback): void
显示输入法软键盘。使用callback异步回调。
说明
该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用显示当前输入法的软键盘。
需要权限: ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.showSoftKeyboard((err: BusinessError) => { if (!err) { console.log(\'Succeeded in showing softKeyboard.\'); } else { console.error(`Failed to show softKeyboard: ${JSON.stringify(err)}`); }})
showSoftKeyboard9+
showSoftKeyboard(): Promise
显示输入法软键盘。使用Promise异步回调。
说明
该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用显示当前输入法的软键盘。
需要权限: ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.showSoftKeyboard().then(() => { console.log(\'Succeeded in showing softKeyboard.\');}).catch((err: BusinessError) => { console.error(`Failed to show softKeyboard: ${JSON.stringify(err)}`);});
hideSoftKeyboard9+
hideSoftKeyboard(callback: AsyncCallback): void
隐藏输入法软键盘。使用callback异步回调。
说明
该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用隐藏当前输入法的软键盘。
需要权限: ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.hideSoftKeyboard((err: BusinessError) => { if (!err) { console.log(\'Succeeded in hiding softKeyboard.\'); } else { console.error(`Failed to hide softKeyboard: ${JSON.stringify(err)}`); }})
hideSoftKeyboard9+
hideSoftKeyboard(): Promise
隐藏输入法软键盘。使用Promise异步回调。
说明
该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用隐藏当前输入法的软键盘。
需要权限: ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.hideSoftKeyboard().then(() => { console.log(\'Succeeded in hiding softKeyboard.\');}).catch((err: BusinessError) => { console.error(`Failed to hide softKeyboard: ${JSON.stringify(err)}`);});
stopInput(deprecated)
stopInput(callback: AsyncCallback): void
结束输入会话。使用callback异步回调。
说明
该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。
从API version 6开始支持,从API version 9开始废弃,建议使用stopInputSession()替代。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.stopInput((err: BusinessError, result: boolean) => { if (err) { console.error(`Failed to stopInput: ${JSON.stringify(err)}`); return; } if (result) { console.log(\'Succeeded in stopping input.\'); } else { console.error(\'Failed to stopInput.\'); }});
stopInput(deprecated)
stopInput(): Promise
结束输入会话。使用promise异步回调。
说明
该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。
从API version 6开始支持,从API version 9开始废弃,建议使用stopInputSession()替代。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodController.stopInput().then((result: boolean) => { if (result) { console.log(\'Succeeded in stopping input.\'); } else { console.error(\'Failed to stopInput.\'); }}).catch((err: BusinessError) => { console.error(`Failed to stopInput: ${JSON.stringify(err)}`);})
on(\'insertText\')10+
on(type: \'insertText\', callback: (text: string) => void): void
订阅输入法应用插入文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回需要插入的文本内容。
根据传入的文本,在回调函数中操作编辑框中的内容。
错误码:
以下错误码的详细介绍。
示例:
function callback1(text: string) { console.info(\'Succeeded in getting callback1 data: \' + JSON.stringify(text));}function callback2(text: string) { console.info(\'Succeeded in getting callback2 data: \' + JSON.stringify(text));}try { inputMethodController.on(\'insertText\', callback1); inputMethodController.on(\'insertText\', callback2); //仅取消insertText的callback1的回调 inputMethodController.off(\'insertText\', callback1); //取消insertText的所有回调 inputMethodController.off(\'insertText\');} catch(err) { console.error(`Failed to subscribe insertText: ${JSON.stringify(err)}`);}
off(\'insertText\')10+
off(type: \'insertText\', callback?: (text: string) => void): void
取消订阅输入法应用插入文本事件。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
let onInsertTextCallback = (text: string) => { console.log(`Succeeded in subscribing insertText: ${text}`);};inputMethodController.off(\'insertText\', onInsertTextCallback);inputMethodController.off(\'insertText\');
on(\'deleteLeft\')10+
on(type: \'deleteLeft\', callback: (length: number) => void): void
订阅输入法应用向左删除事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回需要向左删除的文本长度。
根据传入的删除长度,在回调函数中操作编辑框中的文本。
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'deleteLeft\', (length: number) => { console.log(`Succeeded in subscribing deleteLeft, length: ${length}`); });} catch(err) { console.error(`Failed to subscribe deleteLeft: ${JSON.stringify(err)}`);}
off(\'deleteLeft\')10+
off(type: \'deleteLeft\', callback?: (length: number) => void): void
取消订阅输入法应用向左删除文本事件。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
let onDeleteLeftCallback = (length: number) => { console.log(`Succeeded in subscribing deleteLeft, length: ${length}`);};inputMethodController.off(\'deleteLeft\', onDeleteLeftCallback);inputMethodController.off(\'deleteLeft\');
on(\'deleteRight\')10+
on(type: \'deleteRight\', callback: (length: number) => void): void
订阅输入法应用向右删除事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回需要向右删除的文本长度。
根据传入的删除长度,在回调函数中操作编辑框中的文本。
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'deleteRight\', (length: number) => { console.log(`Succeeded in subscribing deleteRight, length: ${length}`); });} catch(err) { console.error(`Failed to subscribe deleteRight: ${JSON.stringify(err)}`);}
off(\'deleteRight\')10+
off(type: \'deleteRight\', callback?: (length: number) => void): void
取消订阅输入法应用向右删除文本事件。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
let onDeleteRightCallback = (length: number) => { console.log(`Succeeded in subscribing deleteRight, length: ${length}`);};inputMethodController.off(\'deleteRight\', onDeleteRightCallback);inputMethodController.off(\'deleteRight\');
on(\'sendKeyboardStatus\')10+
on(type: \'sendKeyboardStatus\', callback: (keyboardStatus: KeyboardStatus) => void): void
订阅输入法应用发送输入法软键盘状态事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回软键盘状态。
根据传入的软键盘状态,在回调函数中做相应操作。
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'sendKeyboardStatus\', (keyboardStatus: inputMethod.KeyboardStatus) => { console.log(`Succeeded in subscribing sendKeyboardStatus, keyboardStatus: ${keyboardStatus}`); });} catch(err) { console.error(`Failed to subscribe sendKeyboardStatus: ${JSON.stringify(err)}`);}
off(\'sendKeyboardStatus\')10+
off(type: \'sendKeyboardStatus\', callback?: (keyboardStatus: KeyboardStatus) => void): void
取消订阅输入法应用发送软键盘状态事件。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
示例:
let onSendKeyboardStatus = (keyboardStatus: inputMethod.KeyboardStatus) => { console.log(`Succeeded in subscribing sendKeyboardStatus, keyboardStatus: ${keyboardStatus}`);};inputMethodController.off(\'sendKeyboardStatus\', onSendKeyboardStatus);inputMethodController.off(\'sendKeyboardStatus\');
on(\'sendFunctionKey\')10+
on(type: \'sendFunctionKey\', callback: (functionKey: FunctionKey) => void): void
订阅输入法应用发送功能键事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回输入法应用发送的功能键信息。
根据返回的功能键信息,做相应操作。
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'sendFunctionKey\', (functionKey: inputMethod.FunctionKey) => { console.log(`Succeeded in subscribing sendFunctionKey, functionKey.enterKeyType: ${functionKey.enterKeyType}`); });} catch(err) { console.error(`Failed to subscribe sendFunctionKey: ${JSON.stringify(err)}`);}
off(\'sendFunctionKey\')10+
off(type: \'sendFunctionKey\', callback?: (functionKey: FunctionKey) => void): void
取消订阅输入法应用发送功能键事件。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
let onSendFunctionKey = (functionKey: inputMethod.FunctionKey) => { console.log(`Succeeded in subscribing sendFunctionKey, functionKey: ${functionKey.enterKeyType}`);};inputMethodController.off(\'sendFunctionKey\', onSendFunctionKey);inputMethodController.off(\'sendFunctionKey\');
on(\'moveCursor\')10+
on(type: \'moveCursor\', callback: (direction: Direction) => void): void
订阅输入法应用移动光标事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回光标信息。
根据返回的光标移动方向,改变光标位置,如光标向上或向下。
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'moveCursor\', (direction: inputMethod.Direction) => { console.log(`Succeeded in subscribing moveCursor, direction: ${direction}`); });} catch(err) { console.error(`Failed to subscribe moveCursor: ${JSON.stringify(err)}`);}
off(\'moveCursor\')10+
off(type: \'moveCursor\', callback?: (direction: Direction) => void): void
取消订阅输入法应用移动光标事件。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
let onMoveCursorCallback = (direction: inputMethod.Direction) => { console.log(`Succeeded in subscribing moveCursor, direction: ${direction}`);};inputMethodController.off(\'moveCursor\', onMoveCursorCallback);inputMethodController.off(\'moveCursor\');
on(\'handleExtendAction\')10+
on(type: \'handleExtendAction\', callback: (action: ExtendAction) => void): void
订阅输入法应用发送扩展编辑操作事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回扩展编辑操作类型。
根据传入的扩展编辑操作类型,做相应的操作,如剪切、复制等。
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'handleExtendAction\', (action: inputMethod.ExtendAction) => { console.log(`Succeeded in subscribing handleExtendAction, action: ${action}`); });} catch(err) { console.error(`Failed to subscribe handleExtendAction: ${JSON.stringify(err)}`);}
off(\'handleExtendAction\')10+
off(type: \'handleExtendAction\', callback?: (action: ExtendAction) => void): void
取消订阅输入法应用发送扩展编辑操作事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
try { let onHandleExtendActionCallback = (action: inputMethod.ExtendAction) => { console.log(`Succeeded in subscribing handleExtendAction, action: ${action}`); }; inputMethodController.off(\'handleExtendAction\', onHandleExtendActionCallback); inputMethodController.off(\'handleExtendAction\');} catch(err) { console.error(`Failed to subscribe handleExtendAction: ${JSON.stringify(err)}`);}
on(\'selectByRange\')10+
on(type: \'selectByRange\', callback: Callback): void
订阅输入法应用按范围选中文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回需要选中的文本范围。
根据传入的文本范围,开发者在回调函数中编辑框中相应文本。
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'selectByRange\', (range: inputMethod.Range) => { console.log(`Succeeded in subscribing selectByRange: start: ${range.start} , end: ${range.end}`); });} catch(err) { console.error(`Failed to subscribe selectByRange: ${JSON.stringify(err)}`);}
off(\'selectByRange\')10+
off(type: \'selectByRange\', callback?: Callback): void
取消订阅输入法应用按范围选中文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
try { let onSelectByRangeCallback = (range: inputMethod.Range) => { console.log(`Succeeded in subscribing selectByRange, start: ${range.start} , end: ${range.end}`); }; inputMethodController.off(\'selectByRange\', onSelectByRangeCallback); inputMethodController.off(\'selectByRange\');} catch(err) { console.error(`Failed to subscribe selectByRange: ${JSON.stringify(err)}`);}
on(\'selectByMovement\')10+
on(type: \'selectByMovement\', callback: Callback): void
订阅输入法应用按光标移动方向,选中文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
回调函数,返回光标移动的方向。
根据传入的光标移动方向,选中编辑框中相应文本。
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'selectByMovement\', (movement: inputMethod.Movement) => { console.log(\'Succeeded in subscribing selectByMovement: direction: \' + movement.direction); });} catch(err) { console.error(`Failed to subscribe selectByMovement: ${JSON.stringify(err)}`);}
off(\'selectByMovement\')10+
off(type: \'selectByMovement\', callback?: Callback): void
取消订阅输入法应用按光标移动方向,选中文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
try { let onSelectByMovementCallback = (movement: inputMethod.Movement) => { console.log(`Succeeded in subscribing selectByMovement, movement.direction: ${movement.direction}`); }; inputMethodController.off(\'selectByMovement\', onSelectByMovementCallback); inputMethodController.off(\'selectByMovement\');} catch(err) { console.error(`Failed to unsubscribing selectByMovement: ${JSON.stringify(err)}`);}
on(\'getLeftTextOfCursor\')10+
on(type: \'getLeftTextOfCursor\', callback: (length: number) => string): void
订阅输入法应用获取光标左侧指定长度文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'getLeftTextOfCursor\', (length: number) => { console.info(`Succeeded in subscribing getLeftTextOfCursor, length: ${length}`); let text:string = \"\"; return text; });} catch(err) { console.error(`Failed to unsubscribing getLeftTextOfCursor. err: ${JSON.stringify(err)}`);}
off(\'getLeftTextOfCursor\')10+
off(type: \'getLeftTextOfCursor\', callback?: (length: number) => string): void
取消订阅输入法应用获取光标左侧指定长度文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
try { let getLeftTextOfCursorCallback = (length: number) => { console.info(`Succeeded in unsubscribing getLeftTextOfCursor, length: ${length}`); let text:string = \"\"; return text; }; inputMethodController.off(\'getLeftTextOfCursor\', getLeftTextOfCursorCallback); inputMethodController.off(\'getLeftTextOfCursor\');} catch(err) { console.error(`Failed to unsubscribing getLeftTextOfCursor. err: ${JSON.stringify(err)}`);}
on(\'getRightTextOfCursor\')10+
on(type: \'getRightTextOfCursor\', callback: (length: number) => string): void
订阅输入法应用获取光标右侧指定长度文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'getRightTextOfCursor\', (length: number) => { console.info(`Succeeded in subscribing getRightTextOfCursor, length: ${length}`); let text:string = \"\"; return text; });} catch(err) { console.error(`Failed to subscribe getRightTextOfCursor. err: ${JSON.stringify(err)}`);}
off(\'getRightTextOfCursor\')10+
off(type: \'getRightTextOfCursor\', callback?: (length: number) => string): void
取消订阅输入法应用获取光标右侧指定长度文本事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
try { let getRightTextOfCursorCallback = (length: number) => { console.info(`Succeeded in unsubscribing getRightTextOfCursor, length: ${length}`); let text:string = \"\"; return text; }; inputMethodController.off(\'getRightTextOfCursor\', getRightTextOfCursorCallback); inputMethodController.off(\'getRightTextOfCursor\');} catch(err) { console.error(`Failed to unsubscribing getRightTextOfCursor. err: ${JSON.stringify(err)}`);}
on(\'getTextIndexAtCursor\')10+
on(type: \'getTextIndexAtCursor\', callback: () => number): void
订阅输入法应用获取光标处文本索引事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
try { inputMethodController.on(\'getTextIndexAtCursor\', () => { console.info(`Succeeded in subscribing getTextIndexAtCursor.`); let index:number = 0; return index; });} catch(err) { console.error(`Failed to subscribe getTextIndexAtCursor. err: ${JSON.stringify(err)}`);}
off(\'getTextIndexAtCursor\')10+
off(type: \'getTextIndexAtCursor\', callback?: () => number): void
取消订阅输入法应用获取光标处文本索引事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
取消订阅的回调函数,需要与on接口传入的保持一致。
参数不填写时,取消订阅type对应的所有回调事件。
示例:
try { let getTextIndexAtCursorCallback = () => { console.info(`Succeeded in unsubscribing getTextIndexAtCursor.`); let index:number = 0; return index; }; inputMethodController.off(\'getTextIndexAtCursor\', getTextIndexAtCursorCallback); inputMethodController.off(\'getTextIndexAtCursor\');} catch(err) { console.error(`Failed to unsubscribing getTextIndexAtCursor. err: ${JSON.stringify(err)}`);}
InputMethodSetting8+
下列API均需使用getSetting获取到InputMethodSetting实例后,通过实例调用。
on(\'imeChange\')9+
on(type: \'imeChange\', callback: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void
订阅输入法及子类型变化监听事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
示例:
import { InputMethodSubtype } from \'@kit.IMEKit\';try { inputMethodSetting.on(\'imeChange\', (inputMethodProperty: inputMethod.InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => { console.log(\'Succeeded in subscribing imeChange: inputMethodProperty: \' + JSON.stringify(inputMethodProperty) + \" , inputMethodSubtype: \" + JSON.stringify(inputMethodSubtype)); });} catch(err) { console.error(`Failed to unsubscribing inputMethodProperty. err: ${JSON.stringify(err)}`);}
off(\'imeChange\')9+
off(type: \'imeChange\', callback?: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void
取消订阅输入法及子类型变化监听事件。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
示例:
inputMethodSetting.off(\'imeChange\');
listInputMethodSubtype9+
listInputMethodSubtype(inputMethodProperty: InputMethodProperty, callback: AsyncCallback<Array>): void
获取指定输入法应用的所有子类型。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { InputMethodSubtype } from \'@kit.IMEKit\';import { BusinessError } from \'@kit.BasicServicesKit\';let inputMethodProperty: inputMethod.InputMethodProperty = { name: \'com.example.kikakeyboard\', id: \'propertyId\', packageName: \'com.example.kikakeyboard\', methodId: \'propertyId\',}let inputMethodSetting = inputMethod.getSetting();try { inputMethodSetting.listInputMethodSubtype(inputMethodProperty, (err: BusinessError, data: Array) => { if (err) { console.error(`Failed to listInputMethodSubtype: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in listing inputMethodSubtype.\'); });} catch (err) { console.error(`Failed to listInputMethodSubtype: ${JSON.stringify(err)}`);}
listInputMethodSubtype9+
listInputMethodSubtype(inputMethodProperty: InputMethodProperty): Promise<Array>
获取指定输入法应用的所有子类型。使用promise异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { InputMethodSubtype } from \'@kit.IMEKit\';import { BusinessError } from \'@kit.BasicServicesKit\';let inputMethodProperty: inputMethod.InputMethodProperty = { name: \'com.example.kikakeyboard\', id: \'propertyId\', packageName: \'com.example.kikakeyboard\', methodId: \'propertyId\',}let inputMethodSetting = inputMethod.getSetting();try { inputMethodSetting.listInputMethodSubtype(inputMethodProperty).then((data: Array) => { console.log(\'Succeeded in listing inputMethodSubtype.\'); }).catch((err: BusinessError) => { console.error(`Failed to listInputMethodSubtype: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to listInputMethodSubtype: ${JSON.stringify(err)}`);}
listCurrentInputMethodSubtype9+
listCurrentInputMethodSubtype(callback: AsyncCallback<Array>): void
查询当前输入法应用的所有子类型。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { InputMethodSubtype } from \'@kit.IMEKit\';import { BusinessError } from \'@kit.BasicServicesKit\';let inputMethodSetting = inputMethod.getSetting();try { inputMethodSetting.listCurrentInputMethodSubtype((err: BusinessError, data: Array) => { if (err) { console.error(`Failed to listCurrentInputMethodSubtype: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in listing currentInputMethodSubtype.\'); });} catch(err) { console.error(`Failed to listCurrentInputMethodSubtype: ${JSON.stringify(err)}`);}
listCurrentInputMethodSubtype9+
listCurrentInputMethodSubtype(): Promise<Array>
查询当前输入法应用的所有子类型。使用promise异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { InputMethodSubtype } from \'@kit.IMEKit\';import { BusinessError } from \'@kit.BasicServicesKit\';let inputMethodSetting = inputMethod.getSetting();try { inputMethodSetting.listCurrentInputMethodSubtype().then((data: Array) => { console.log(\'Succeeded in listing currentInputMethodSubtype.\'); }).catch((err: BusinessError) => { console.error(`Failed to listCurrentInputMethodSubtype: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to listCurrentInputMethodSubtype: ${JSON.stringify(err)}`);}
getInputMethods9+
getInputMethods(enable: boolean, callback: AsyncCallback<Array>): void
获取已激活/未激活的输入法应用列表。使用callback异步回调。
说明
已激活输入法为使能的输入法应用。默认输入法默认使能,其他输入法可被设置为使能或非使能。
已激活输入法列表包括默认输入法和已被设置为使能的输入法应用,未激活输入法列表包括除使能输入法以外的其他已安装的输入法。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { inputMethodSetting.getInputMethods(true, (err: BusinessError, data: Array) => { if (err) { console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in getting inputMethods.\'); });} catch (err) { console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`);}
getInputMethods9+
getInputMethods(enable: boolean): Promise<Array>
获取已激活/未激活的输入法应用列表。使用promise异步回调。
说明
已激活输入法为使能的输入法应用。默认输入法默认使能,其他输入法可被设置为使能或非使能。
已激活输入法列表包括默认输入法和已被设置为使能的输入法应用,未激活输入法列表包括除使能输入法以外的其他已安装的输入法。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍请。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { inputMethodSetting.getInputMethods(true).then((data: Array) => { console.log(\'Succeeded in getting inputMethods.\'); }).catch((err: BusinessError) => { console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`); })} catch(err) { console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`);}
getInputMethodsSync11+
getInputMethodsSync(enable: boolean): Array
获取已激活/未激活的输入法应用列表。同步接口。
说明
已激活输入法为使能的输入法应用。默认输入法默认使能,其他输入法可被设置为使能或非使能。
已激活输入法列表包括默认输入法和已被设置为使能的输入法应用,未激活输入法列表包括除使能输入法以外的其他已安装的输入法。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
返回值:
错误码:
以下错误码的详细介绍。
示例:
try { let imeProp = inputMethodSetting.getInputMethodsSync(true);} catch(err) { console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`);}
getAllInputMethods11+
getAllInputMethods(callback: AsyncCallback<Array>): void
获取所有输入法应用列表。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { inputMethodSetting.getAllInputMethods((err: BusinessError, data: Array) => { if (err) { console.error(`Failed to getAllInputMethods: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in getting all inputMethods.\'); });} catch (err) { console.error(`Failed to getAllInputMethods: ${JSON.stringify(err)}`);}
getAllInputMethods11+
getAllInputMethods(): Promise<Array>
获取所有输入法应用列表。使用promise异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodSetting.getAllInputMethods().then((data: Array) => { console.log(\'Succeeded in getting all inputMethods.\');}).catch((err: BusinessError) => { console.error(`Failed to getAllInputMethods: ${JSON.stringify(err)}`);})
getAllInputMethodsSync11+
getAllInputMethodsSync(): Array
获取所有输入法应用列表。同步接口。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
try { let imeProp = inputMethodSetting.getAllInputMethodsSync();} catch(err) { console.error(`Failed to getAllInputMethodsSync: ${JSON.stringify(err)}`);}
showOptionalInputMethods9+
showOptionalInputMethods(callback: AsyncCallback): void
显示输入法选择对话框。使用callback异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';try { inputMethodSetting.showOptionalInputMethods((err: BusinessError, data: boolean) => { if (err) { console.error(`Failed to showOptionalInputMethods: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in showing optionalInputMethods.\'); });} catch (err) { console.error(`Failed to showOptionalInputMethods: ${JSON.stringify(err)}`);}
showOptionalInputMethods9+
showOptionalInputMethods(): Promise
显示输入法选择对话框。使用promise异步回调。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
错误码:
以下错误码的详细介绍。
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodSetting.showOptionalInputMethods().then((data: boolean) => { console.log(\'Succeeded in showing optionalInputMethods.\');}).catch((err: BusinessError) => { console.error(`Failed to showOptionalInputMethods: ${JSON.stringify(err)}`);})
listInputMethod(deprecated)
listInputMethod(callback: AsyncCallback<Array>): void
查询已安装的输入法列表。使用callback异步回调。
说明
从API version 8开始支持,从API version 9开始废弃,建议使用getInputMethods替代。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodSetting.listInputMethod((err: BusinessError, data: Array) => { if (err) { console.error(`Failed to listInputMethod: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in listing inputMethod.\'); });
listInputMethod(deprecated)
listInputMethod(): Promise<Array>
查询已安装的输入法列表。使用promise异步回调。
说明
从API version 8开始支持,从API version 9开始废弃,建议使用getInputMethods替代。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodSetting.listInputMethod().then((data: Array) => { console.log(\'Succeeded in listing inputMethod.\');}).catch((err: BusinessError) => { console.error(`Failed to listInputMethod: ${JSON.stringify(err)}`);})
displayOptionalInputMethod(deprecated)
displayOptionalInputMethod(callback: AsyncCallback): void
显示输入法选择对话框。使用callback异步回调。
说明
从API version 8开始支持,从API version 9开始废弃,建议使用showOptionalInputMethods()替代。
系统能力: SystemCapability.MiscServices.InputMethodFramework
参数:
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodSetting.displayOptionalInputMethod((err: BusinessError) => { if (err) { console.error(`Failed to displayOptionalInputMethod: ${JSON.stringify(err)}`); return; } console.log(\'Succeeded in displaying optionalInputMethod.\');});
displayOptionalInputMethod(deprecated)
displayOptionalInputMethod(): Promise
显示输入法选择对话框。使用promise异步回调。
说明
从API version 8开始支持,从API version 9开始废弃,建议使用showOptionalInputMethods()替代。
系统能力: SystemCapability.MiscServices.InputMethodFramework
返回值:
示例:
import { BusinessError } from \'@kit.BasicServicesKit\';inputMethodSetting.displayOptionalInputMethod().then(() => { console.log(\'Succeeded in displaying optionalInputMethod.\');}).catch((err: BusinessError) => { console.error(`Failed to displayOptionalInputMethod: ${JSON.stringify(err)}`);})