This article aims to deeply explore the technical details of Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices.
Mainly serving as a carrier for technical sharing and exchange, it is inevitable that there may be errors and omissions. We welcome valuable opinions and questions from colleagues to make progress together.
This article is original content. Any form of reprint must indicate the source and original author.
As an operating system independently developed by Huawei, HarmonyOS brings a brand-new development experience to developers with its characteristics such as distributed technology and multi-device collaboration. And IME Kit, as the input method development service in HarmonyOS, enables developers to easily build input method applications with rich functions and bring more convenient input experiences to users. This article will take you into the world of HarmonyOS input method development, understand the basic concepts of IME Kit, development environment configuration, function implementation, and input method switching operations, helping you quickly build your first input method application.
Introduction to IME Kit
IME Kit is responsible for establishing a communication channel between the application where the edit box is located and the input method application to ensure that the two can work together to provide text input functions and also provides the ability for system applications to manage input method applications. It mainly contains two major types of APIs:
- Input method service API: Used for input method applications, including creating soft keyboard windows, inserting/deleting characters, selecting text, listening for physical keyboard key events, etc.
- Input method framework API: Can be used for custom-drawn edit boxes, including binding input methods, implementing input, deletion, selection, cursor movement, etc.
Development environment configuration
To start HarmonyOS input method development, you need to install the following development tools:
- DevEco Studio: The official integrated development environment for HarmonyOS.
- Java Development Kit (JDK): Java development toolkit.
- HarmonyOS SDK: HarmonyOS software development kit. After installing the development tools, you need to create a HarmonyOS application project and select "InputMethodExtensionAbility" as the extended capability.
Implementation of basic capabilities of input method applications
InputMethodExtensionAbility is the core component of input method applications. It has two lifecycle methods, onCreate()
and onDestroy()
, which are used for initialization and destruction operations respectively.
onCreate() method:
- Register event listeners: Use the
inputMethodAbility.on()
method to register event listeners for the input method framework, such asinputStart
andinputStop
events, so as to perform corresponding processing when the input method starts and stops. - Initialize the window: Use the
inputMethodAbility.createPanel()
method to create an input method window and set the window type, position, size, and content. You can use ArkUI components to build the window interface, such as usingStack
,Flex
, andText
components to create a keyboard layout.
onDestroy() method:
- Unregister event listeners: Use the
inputMethodAbility.off()
method to unregister the previously registered event listeners to avoid memory leaks. - Destroy the window: Use the
inputMethodAbility.destroyPanel()
method to destroy the input method window and release resources.
Sample code:
import { Want } from '@kit.AbilityKit';
import keyboardController from './model/KeyboardController';
import { InputMethodExtensionAbility } from '@kit.IMEKit';
export default class InputDemoService extends InputMethodExtensionAbility {
onCreate(want: Want): void {
keyboardController.onCreate(this.context); // Initialize the window and register event listeners for the input method framework
}
onDestroy(): void {
console.log("onDestroy.");
keyboardController.onDestroy(); // Destroy the window and unregister event listeners
}
}
Implementation of input method switching
IME Kit provides the switchInputMethod()
method for switching between different input method applications.
Implementation steps:
-
Get all input method applications: Use the
inputMethod.getSetting().getInputMethods(true)
method to obtain a list of all enabled input method applications. -
Get the current input method application: Use the
inputMethod.getCurrentInputMethod()
method to obtain the current input method application. -
Switch input method applications: Traverse all input method applications and use the
inputMethod.switchInputMethod()
method to switch to the target input method application.
Sample code:
import { inputMethod } from '@kit.IMEKit';
export class KeyboardController {
async switchInputMethod(){
let inputMethods = await inputMethod.getSetting().getInputMethods(true); // Obtain the list of enabled input methods
let currentInputMethod = inputMethod.getCurrentInputMethod(); // Obtain the current input method
for(let i=0;i<inputMethods.length;i++) {
if(inputMethods[i].name!= currentInputMethod.name) { // When it is determined that it is not the current input method, switch to that input method. In actual development, you can switch to a fixed input method.
await inputMethod.switchInputMethod(inputMethods[i]);
}
}
}
}
Summary
IME Kit provides powerful support for HarmonyOS input method development. As developers, we can use the APIs and components it provides to easily build input method applications with rich functions. This article introduced the basic concepts of IME Kit, development environment configuration, function implementation, and input method switching operations, helping you quickly get started with HarmonyOS input method development.
Or you can also try:
- Create a simple keyboard application to implement the input functions of number keys and letter keys.
- Add more functions, such as symbol keys, emoticons, voice input, etc.
- Optimize the keyboard interface to make it more beautiful and easy to use.
Top comments (0)