harmony 鸿蒙Combination Key Development
Combination Key Development
When to Use
The inputConsumer module provides capabilities such as subscribing to combination key events and setting the key shielding status. For example, if an application needs to implement a shortcut function using combination keys, you can listen for combination key events to serve that purpose.
Modules to Import
import { inputConsumer } from '@kit.InputKit';
Available APIs
The following table lists the common APIs provided by the inputConsumer module. For details, see ohos.multimodalInput.inputConsumer-sys and ohos.multimodalInput.inputConsumer.
API | Description |
---|---|
on(type: ‘key’, keyOptions: KeyOptions, callback: Callback<KeyOptions>): void | Enables listening for combination key events. |
off(type: ‘key’, keyOptions: KeyOptions, callback?: Callback<KeyOptions>): void | Disables listening for combination key events. |
setShieldStatus(shieldMode: ShieldMode, isShield: boolean): void | Sets the key shielding status. |
getShieldStatus(shieldMode: ShieldMode): boolean | Checks whether key shielding is enabled. |
getAllSystemHotkeys(): Promise<Array<HotkeyOptions>> | Obtains all system combination keys. |
on(type: ‘hotkeyChange’, hotkeyOptions: HotkeyOptions, callback: Callback<HotkeyOptions>): void | Enables listening for global combination key events. |
off(type: ‘hotkeyChange’, hotkeyOptions: HotkeyOptions, callback?: Callback<HotkeyOptions>): void | Disables listening for global combination key events. |
How to Develop
When an application that uses specific combination keys is started, on is called to subscribe to combination key events.
When the application is stopped, off is called to unsubscribe from combination key events.
let leftAltKey = 2045;
let tabKey = 2049;
let callback = (keyOptions: inputConsumer.KeyOptions) => {
console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
}
// Start the application.
let keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
try {
inputConsumer.on("key", keyOption, callback);// Listen for combination key events.
} catch (error) {
console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// Stop the application.
try {
inputConsumer.off("key", keyOption, callback);// Disable listening for combination key events.
console.log(`Unsubscribe success`);
} catch (error) {
console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
let leftCtrlKey = 2072;
let zKey = 2042;
let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => {
console.log(`keyOptions: ${JSON.stringify(hotkeyOptions)}`);
}
let hotkeyOption: inputConsumer.HotkeyOptions = {preKeys: [leftCtrlKey], finalKey: zKey, isRepeat: false};
// Before subscribing to global combination keys, obtain all system combination keys and check whether the combination keys to subscribe are on the system combination key list.
inputConsumer.getAllSystemHotkeys().then((data: Array<inputConsumer.HotkeyOptions>) => {//: Obtain all system combination keys.
console.log(`List of system hotkeys : ${JSON.stringify(data)}`);
});
// Start the application.
try {
inputConsumer.on("hotkeyChange", hotkeyOption, hotkeyCallback);// Subscribe to global combination keys.
} catch (error) {
console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// Stop the application.
try {
inputConsumer.off("hotkeyChange", hotkeyOption, hotkeyCallback);// Unsubscribe from global combination keys.
console.log(`Unsubscribe success`);
} catch (error) {
console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Input Kit (Multimodal Input)
harmony 鸿蒙Introduction to Input Kit
harmony 鸿蒙Input Device Development
harmony 鸿蒙Event Injection Development
harmony 鸿蒙Input Monitor Development
harmony 鸿蒙Event Interception Development (C/C++)
harmony 鸿蒙Event Listening Development (C/C++)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦