openharmony 鸿蒙 inputconsumer-guidelines-sys

2026-08-25 浏览 (1)

Global Hotkey Development (for System Applications Only)

When to Use

The inputConsumer module implements global hotkey management, such as subscribing to global hotkeys and setting the key shielding status. By subscribing to global hotkeys, you can use the corresponding key combinations to activate shortcut functions.

Specifically, global hotkeys are combination keys defined by either the system or applications; system hotkeys are those defined by the system, while application hotkeys are those defined by applications.

Modules to Import

import { inputConsumer } from '@kit.InputKit';

Available APIs

The following table lists common APIs for global shortcut key management. For details, see @ohos.multimodalInput.inputConsumer (Global Shortcut Keys) (System API) and @ohos.multimodalInput.inputConsumer (Global Shortcut Keys).

APIDescription
on(type: 'key', keyOptions: KeyOptions, callback: Callback<KeyOptions>): voidSubscribes to system hotkey change events.
off(type: 'key', keyOptions: KeyOptions, callback?: Callback<KeyOptions>): voidUnsubscribes from system hotkey change events.
setShieldStatus(shieldMode: ShieldMode, isShield: boolean): voidSets the key shielding status.
getShieldStatus(shieldMode: ShieldMode): booleanChecks whether key shielding is enabled.
getAllSystemHotkeys(): Promise<Array<HotkeyOptions>>Obtains all system hotkeys.
on(type: 'hotkeyChange', hotkeyOptions: HotkeyOptions, callback: Callback<HotkeyOptions>): voidSubscribes to application hotkey change events.
off(type: 'hotkeyChange', hotkeyOptions: HotkeyOptions, callback?: Callback<HotkeyOptions>): voidUnsubscribes from application hotkey change events.

How to Develop

When an application that uses specific combination keys is started, call on to subscribe to combination key events. When the application is stopped, call off to unsubscribe from combination key events.

import { inputConsumer } from '@kit.InputKit';

@Entry
@Component
struct Index {
  build() {
    RelativeContainer() {
      Text()
        .onClick(() => {
          let leftAltKey = 2045;
          let tabKey = 2049;
          let callback = (keyOptions: inputConsumer.KeyOptions) => {
            console.info(`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); // Subscribe to system hotkey change events.
          } catch (error) {
            console.error(`Execute failed, error: ${JSON.stringify(error, ["code", "message"])}`);
          }
          // Stop the application.
          try {
            inputConsumer.off("key", keyOption, callback); // Unsubscribe from system hotkey change events.
            console.info(`Unsubscribe success`);
          } catch (error) {
            console.error(`Execute failed, error: ${JSON.stringify(error, ["code", "message"])}`);
          }
        })
    }
  }
}
import { inputConsumer } from '@kit.InputKit';

@Entry
@Component
struct Index {
  build() {
    RelativeContainer() {
      Text()
        .onClick(() => {
          let leftCtrlKey = 2072;
          let zKey = 2042;
          let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => {
            console.info(`keyOptions: ${JSON.stringify(hotkeyOptions)}`);
          }
          let hotkeyOption: inputConsumer.HotkeyOptions = { preKeys: [leftCtrlKey], finalKey: zKey, isRepeat: false };
          // Before subscribing to change events of the specified hotkey, you need to check whether the hotkey exists in the system hotkey list to avoid conflicts.
          inputConsumer.getAllSystemHotkeys().then((data: Array<inputConsumer.HotkeyOptions>) => { // Obtain all system hotkeys.
            console.info(`List of system hotkeys : ${JSON.stringify(data)}`);
          });
          // Start the application.
          try {
            inputConsumer.on("hotkeyChange", hotkeyOption, hotkeyCallback); // Subscribe to change events of the hotkey.
          } catch (error) {
            console.error(`Execute failed, error: ${JSON.stringify(error, ["code", "message"])}`);
          }
          // Stop the application.
          try {
            inputConsumer.off("hotkeyChange", hotkeyOption, hotkeyCallback); // Unsubscribe from change events of the hotkey.
            console.info(`Unsubscribe success`);
          } catch (error) {
            console.error(`Execute failed, error: ${JSON.stringify(error, ["code", "message"])}`);
          }
        })
    }
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 keypressed-guidelines

openharmony 鸿蒙 shortkey-guidelines-sys

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 inputeventclient-guidelines-sys

openharmony 鸿蒙 inputdevice-guidelines

openharmony 鸿蒙 interceptor-guidelines

openharmony 鸿蒙 pointerstyle-guidelines

openharmony 鸿蒙 input-overview

openharmony 鸿蒙 inputmonitor-guidelines-sys

openharmony 鸿蒙 monitor-guidelines

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