openharmony 鸿蒙 js-apis-inputmethod-sys

2025-06-16 浏览 (1)

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

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

说明:

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

导入模块

import { inputMethod } from '@kit.IMEKit';

inputMethod.switchInputMethod11+

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

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

需要权限: ohos.permission.CONNECT_IME_ABILITY

系统能力: SystemCapability.MiscServices.InputMethodFramework

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

参数:

参数名类型必填说明
bundleNamestring是目标输入法包名。
subtypeIdstring否输入法子类型。

返回值:

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

错误码:

以下错误码的详细介绍请参见输入法框架错误码,通用错误码说明文档。

错误码ID错误信息
201permissions check fails.
202not system application.
401parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
12800005configuration persistence error.
12800008input method manager service error.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let currentIme = inputMethod.getCurrentInputMethod();
try {
  inputMethod.switchInputMethod(currentIme.name).then(() => {
    console.info('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.info('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

参数:

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

错误码:

以下错误码的详细介绍请参见通用错误码说明文档。

错误码ID错误信息
202not system application.

示例:

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

参数:

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

错误码:

以下错误码的详细介绍请参见通用错误码说明文档。

错误码ID错误信息
202not system application.

示例:

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

参数:

参数名类型必填说明
typestring是设置监听类型,固定取值'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

参数:

参数名类型必填说明
typestring是设置监听类型,固定取值'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

参数:

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

返回值:

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

错误码:

以下错误码的详细介绍请参见输入法框架错误码,通用错误码说明文档。

错误码ID错误信息
202not system application.
401parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
12800008input method manager service error.

示例:

import { PanelInfo, PanelType, PanelFlag } from '@kit.IMEKit';

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

enableInputMethod20+

enableInputMethod(bundleName: string, extensionName: string, enabledState: EnabledState): Promise<void>

修改输入法的启用状态。使用promise异步回调。

需要权限: ohos.permission.CONNECT_IME_ABILITY

系统能力: SystemCapability.MiscServices.InputMethodFramework

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

参数:

参数名类型必填说明
bundleNamestring是输入法包名。
extensionNamestring是输入法扩展名。
enabledStateEnabledState是输入法启用状态。

返回值:

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

错误码:

以下错误码的详细介绍请参见输入法框架错误码,通用错误码说明文档。

错误码ID错误信息
201permissions check fails.
202not system application.
12800008input method manager service error.
12800018the input method is not found.
12800019current operation cannot be applied to the preconfigured default input method.

示例:

import { BusinessError } from '@kit.BasicServicesKit';

let currentIme = inputMethod.getCurrentInputMethod();
try {
  inputMethodSetting.enableInputMethod(currentIme.name, currentIme.id, inputMethod.EnabledState.BASIC_MODE).then(() => {
    console.info('Succeeded in enable inputmethod.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to enableInputMethod. Code: ${err.code}, message: ${err.message}`);
  })
} catch (err) {
  console.error(`Failed to enableInputMethod. Code: ${err.code}, message: ${err.message}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙IME Kit(输入法开发服务)

harmony 鸿蒙InputMethod

harmony 鸿蒙输入法框架错误码

harmony 鸿蒙inputmethod_attach_options_capi.h

harmony 鸿蒙inputmethod_controller_capi.h

harmony 鸿蒙inputmethod_cursor_info_capi.h

harmony 鸿蒙inputmethod_inputmethod_proxy_capi.h

harmony 鸿蒙inputmethod_private_command_capi.h

harmony 鸿蒙inputmethod_text_avoid_info_capi.h

harmony 鸿蒙inputmethod_text_config_capi.h

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/FQ74ql