@ohos.inputMethod (输入法框架) (系统接口)

本模块主要面向普通前台应用(备忘录、信息、设置等系统应用与三方应用),提供对输入法(输入法应用)的控制、管理能力,包括显示/隐藏输入法软键盘、切换输入法、获取所有输入法列表等等。

说明:

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

导入模块

import inputMethod from '@ohos.inputMethod';

inputMethod.switchInputMethod11+

switchInputMethod(bundleName: string, subtypeId?: string): Promise<void>

切换输入法,使用promise异步回调。

需要权限: ohos.permission.CONNECT_IME_ABILITY

系统能力: SystemCapability.MiscServices.InputMethodFramework

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
bundleName string 目标输入法包名。
subtypeId string 输入法子类型。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID 错误信息
12800005 configuration persisting error.
12800008 input method manager service error.

示例:

import { BusinessError } from '@ohos.base';

let currentIme = inputMethod.getCurrentInputMethod();
try {
  inputMethod.switchInputMethod(currentIme.name).then(() => {
    console.log('Succeeded in switching inputmethod.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`);
  })
} catch (err) {
  console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`);
}
let currentImeSubType = inputMethod.getCurrentInputMethodSubtype();
try {
  inputMethod.switchInputMethod(currentIme.name, currentImeSubType.id).then(() => {
    console.log('Succeeded in switching inputmethod.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`);
  })
} catch (err) {
  console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`);
}

InputMethodSetting8+

下列API均需使用getSetting获取到InputMethodSetting实例后,通过实例调用。

on('imeShow')10+

on(type: 'imeShow', callback: (info: Array<InputWindowInfo>) => void): void

订阅输入法Panel固定态软键盘显示事件。使用callback异步回调。

系统接口:此接口为系统接口。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型,固定取值为'imeShow'。
callback (info: Array<InputWindowInfo>) => void 回调函数,返回输入法固定态软键盘信息。

示例:

try {
  inputMethodSetting.on('imeShow', (info: Array<inputMethod.InputWindowInfo>) => {
    console.info('Succeeded in subscribing imeShow event.');
  });
} catch(err) {
  console.error(`Failed to unsubscribing imeShow. err: ${JSON.stringify(err)}`);
}

on('imeHide')10+

on(type: 'imeHide', callback: (info: Array<InputWindowInfo>) => void): void

订阅输入法Panel固定态软键盘隐藏事件。使用callback异步回调。

系统接口:此接口为系统接口。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型,固定取值为'imeHide'。
callback (info: Array<InputWindowInfo>) => void 回调函数,返回输入法固定态软键盘信息。

示例:

try {
  inputMethodSetting.on('imeHide', (info: Array<inputMethod.InputWindowInfo>) => {
    console.info('Succeeded in subscribing imeHide event.');
  });
} catch(err) {
  console.error(`Failed to unsubscribing imeHide. err: ${JSON.stringify(err)}`);
}

off('imeShow')10+

off(type: 'imeShow', callback?: (info: Array<InputWindowInfo>) => void): void

取消订阅输入法Panel固定态软键盘显示事件。

系统接口:此接口为系统接口。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型,固定取值imeShow
callback (info: Array<InputWindowInfo>) => void 取消订阅的回调函数。
参数不填写时,取消订阅type对应的所有回调事件。

示例:

try {
  inputMethodSetting.off('imeShow');
} catch(err) {
  console.error(`Failed to unsubscribing imeShow. err: ${JSON.stringify(err)}`);
}

off('imeHide')10+

off(type: 'imeHide', callback?: (info: Array<InputWindowInfo>) => void): void

取消订阅输入法Panel固定态软键盘隐藏事件。

系统接口:此接口为系统接口。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名 类型 必填 说明
type string 设置监听类型,固定取值'imeHide'。
callback (info: Array<InputWindowInfo>) => void 取消订阅的回调函数。
参数不填写时,取消订阅type对应的所有回调事件。

示例:

try {
  inputMethodSetting.off('imeHide');
} catch(err) {
  console.error(`Failed to unsubscribing imeHide. err: ${JSON.stringify(err)}`);
}

isPanelShown11+

isPanelShown(panelInfo: PanelInfo): boolean

查询指定类型的输入法面板是否处于显示状态。

系统接口:此接口为系统接口。

系统能力: SystemCapability.MiscServices.InputMethodFramework

参数:

参数名 类型 必填 说明
panelInfo PanelInfo 输入法面板的属性。

返回值:

类型 说明
boolean 面板显隐状态查询结果。
- true表示被查询的输入法面板处于显示状态。
- false表示被查询的输入法面板处于隐藏状态。

错误码:

以下错误码的详细介绍请参见输入法框架错误码

错误码ID 错误信息
12800008 input method manager service error.

示例:

import { PanelInfo, PanelType, PanelFlag } from '@ohos.inputMethod.Panel';

let info: PanelInfo = {
  type: PanelType.SOFT_KEYBOARD,
  flag: PanelFlag.FLAG_FIXED
}
try {
  let result = inputMethodSetting.isPanelShown(info);
  console.log('Succeeded in querying isPanelShown, result: ' + result);
} catch (err) {
  console.error(`Failed to query isPanelShown: ${JSON.stringify(err)}`);
}