@ohos.inputMethod (Input Method Framework) (System API)

The inputMethod module is oriented to common foreground applications (third-party applications and system applications such as Notes, Messaging, and Settings). It provides input method control and management capabilities, including displaying or hiding the soft keyboard, switching between input methods, and obtaining the list of all input methods.

NOTE

The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import inputMethod from '@ohos.inputMethod';

inputMethod.switchInputMethod11+

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

Switches to another input method. This API uses a promise to return the result.

Required permissions: ohos.permission.CONNECT_IME_ABILITY

System capability: SystemCapability.MiscServices.InputMethodFramework

System API: This is a system API.

Parameters

Name Type Mandatory Description
bundleName string Yes Bundle name of the target input method.
subtypeId string No Input method subtype.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

ID Error Message
12800005 configuration persisting error.
12800008 input method manager service error.

Example

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+

In the following API examples, you must first use getSetting to obtain an InputMethodSetting instance, and then call the APIs using the obtained instance.

on('imeShow')10+

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

Subscribes to the soft keyboard show event of the input method panel in the fixed state. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

Name Type Mandatory Description
type string Yes Event type, which is 'imeShow'.
callback (info: Array<InputWindowInfo>) => void Yes Callback invoked to return the soft keyboard information of the input method panel in the fixed state.

Example

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

Subscribes to the soft keyboard hide event of the input method panel in the fixed state. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

Name Type Mandatory Description
type string Yes Event type, which is 'imeHide'.
callback (info: Array<InputWindowInfo>) => void Yes Callback invoked to return the soft keyboard information of the input method panel in the fixed state.

Example

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

Unsubscribes from the soft keyboard show event of the input method panel in the fixed state.

System API: This is a system API.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

Name Type Mandatory Description
type string Yes Event type, which is 'imeShow'.
callback (info: Array<InputWindowInfo>) => void No Callback to unregister.
If this parameter is not specified, this API unregisters all callbacks for the specified event type.

Example

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

Unsubscribes from the soft keyboard hide event of the input method panel in the fixed state.

System API: This is a system API.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

Name Type Mandatory Description
type string Yes Event type, which is 'imeHide'.
callback (info: Array<InputWindowInfo>) => void No Callback to unregister.
If this parameter is not specified, this API unregisters all callbacks for the specified event type.

Example

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

isPanelShown11+

isPanelShown(panelInfo: PanelInfo): boolean

Checks whether the input method panel of a specified type is shown.

System API: This is a system API.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

Name Type Mandatory Description
panelInfo PanelInfo Yes Information about the input method panel.

Return value

Type Description
boolean Whether the input method panel is shown.
- The value true means that the input method panel is shown.
- The value false means that the input method panel is hidden.

Error codes

For details about the error codes, see Input Method Framework Error Codes.

ID Error Message
12800008 input method manager service error.

Example

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)}`);
}