目录

@ohos.InputMethodExtensionAbility (InputMethodExtensionAbility)

The @ohos.InputMethodExtensionAbility module provides the base class definition for the input method ExtensionAbility. It serves as the entry point and lifecycle management framework for developing input method apps.

As the core class module for input method ExtensionAbility, this module defines the InputMethodExtensionAbility class which acts as the base extension class for input method apps. You need to inherit this class and implement the onCreate and onDestroy lifecycle callbacks. The system invokes these callbacks automatically when starting or destroying the input method extension.

This module provides two core capabilities: (1) initialization of the input method app via the onCreate(want) callback. This callback is invoked when the system launches the input method extension, where you complete initialization tasks such as resource loading and panel creation. (2) resource cleanup for the input method app via the onDestroy() callback. This callback is invoked when the system destroys the input method extension, where you release resources. In addition, the context property provides an InputMethodExtensionContext object, allowing you to perform context-level operations such as destroying the extension and launching other apps during the lifecycle.

This module is mandatory for input method app development. The workflow is as follows: inherit InputMethodExtensionAbility → configure ExtensionAbility information in module.json5 → onCreate (initialization) is triggered upon system startup → onDestroy (cleanup) is triggered by system destruction or a proactive developer call to context.destroy().

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

InputMethodExtensionAbility defines only the basic lifecycle callbacks onCreate and onDestroy. The core interaction capabilities of the input method (such as panel creation/destruction, keyboard event listening, and client attachment) are obtained from the InputMethodAbility object provided by the @ohos.inputMethodEngine module inside the onCreate callback. onCreate is the sole entry for acquiring all key objects and creating panels, and initialization must be completed within this callback.

The type of the context property of InputMethodExtensionAbility is InputMethodExtensionContext (from the @ohos.InputMethodExtensionContext module). This represents an association relationship: InputMethodExtensionAbility owns the context capability of InputMethodExtensionContext.

Class Description
InputMethodExtensionAbility Base class of the input method ExtensionAbility, providing the lifecycle management framework for input method apps. Key members include: the context property (an InputMethodExtensionContext object), the onCreate(want) method (initialization callback), and the onDestroy() method (destruction callback). You need to inherit this class and override the lifecycle method.

Constraints

To ensure system security and stability and prevent InputMethodExtensionAbility from abusing system resources, the system imposes capability control. References to certain modules are not supported. For details, see Appendix.

In addition, input method apps are classified into basic mode and full experience mode. The two modes are described as follows:

Basic mode:

In basic mode, the input method extension (InputMethodExtensionAbility) process cannot launch other UIAbilities or ExtensionAbilities.

In basic mode, the input method extension is subject to system control. It cannot use various APIs that access or expose user's personal data, nor can it transfer data out of the process. The controlled features include but are not limited to: network, SMS, telephony, microphone, location, camera, Bluetooth, wallpaper, payment, calendar, game, speaker, Wi-Fi, clipboard, multimedia, contacts, common events, system accounts, health data, map services, push services, unified search, shared memory, distributed features, advertising device identifiers, and vibration.

In basic mode, the input method extension can use the system capabilities necessary for basic input features, such as IME Kit, ArkUI, window, graphics, and screen management.

In basic mode, the input method extension has read-only access to the shared sandbox and read/write access to its dedicated sandbox. The main app entry has read/write access to both the shared sandbox and its dedicated sandbox.

Full experience mode:

In full experience mode, the input method extension is not subject to the restrictions of basic mode. For example, it can launch other UIAbilities or ExtensionAbilities and call APIs that access user data.

In full experience mode, the input method extension has read/write access to the shared sandbox.

Modules to Import

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

InputMethodExtensionAbility

Provides the core capabilities for input method apps and supports you in creating input method apps.

To use the following APIs, you need to create a child class that inherits from InputMethodExtensionAbility and then override or use them in the child class.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.MiscServices.InputMethodFramework

Name Type Read-Only Optional Description
context InputMethodExtensionContext No No Context of the InputMethodExtensionAbility, inherited from ExtensionContext.

Suggestions for using the context parameter:

[h2]onCreate

onCreate(want: Want): void

Lifecycle callback invoked when the input method extension is started for initializing the input method app.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.MiscServices.InputMethodFramework

Parameters

Name Type Mandatory Description
want Want Yes Want type information related to the current extension, including the ability name, bundle name, and more.

Suggestions for using the want parameter:

NOTE

Usage with related APIs: onCreate must be used with the following APIs:

Example

import { InputMethodExtensionAbility, inputMethodEngine } from '@kit.IMEKit';
import { Want } from '@kit.AbilityKit';

class InputMethodExt extends InputMethodExtensionAbility {
  onCreate(want: Want): void {
    console.info(`onCreate, want: ${want.abilityName}`);

    // Obtain the input method capability object.
    let ability: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility();

    // Obtain the keyboard delegate object.
    let keyboardDelegate: inputMethodEngine.KeyboardDelegate = inputMethodEngine.getKeyboardDelegate();

    // Create a panel.
    let panelInfo: inputMethodEngine.PanelInfo = {
      type: inputMethodEngine.PanelType.SOFT_KEYBOARD,
      flag: inputMethodEngine.PanelFlag.FLG_FIXED
    };
    ability.createPanel(this.context, panelInfo, (err, panel) => {
      if (err) {
        console.error(`Failed to create panel: ${err.code}`);
        return;
      }
      console.info('Succeeded in creating panel.');
    });

    // Subscribe to the input method attachment event.
    ability.on('inputStart', (kbController, inputClient) => {
      console.info('Input method bound to client.');
    });
  }
}

[h2]onDestroy

onDestroy(): void

Lifecycle callback invoked when the input method app is destroyed for resource cleanup.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.MiscServices.InputMethodFramework

NOTE

Usage with related APIs: onDestroy must be used with the following APIs:

Example

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

class InputMethodExt extends InputMethodExtensionAbility {
  onDestroy(): void {
    // Destroy the panel, cancel event subscriptions, and perform other cleanup tasks.
    console.info('onDestroy');
  }
}

Appendix

InputMethodExtensionAbility does not support references to the following modules.