1.4K Star 7.4K Fork 8.2K

OpenHarmony/docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
js-apis-inputconsumer.md 10.65 KB
一键复制 编辑 原始数据 按行查看 历史

@ohos.multimodalInput.inputConsumer (Input Consumer)

The inputConsumer module implements listening for combination key events.

NOTE

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

Modules to Import

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

HotkeyOptions14+

Defines shortcut key options.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Name Type Readable Writable Description
preKeys Array<number> Yes No Modifier key set (including Ctrl, Shift, and Alt). A maximum of two modifier keys are supported. There is no requirement on the sequence of modifier keys.
For example, in Ctrl+Shift+Esc, Ctrl and Shift are modifier keys.
finalKey number Yes No Modified key, which is the key other than the modifier key and meta key.
For example, in Ctrl+Shift+Esc, Esc is the modified key.
isRepeat boolean Yes No Whether to report repeated key events. The value true means to report repeated key events, and the value false means the opposite. The default value is true.

KeyPressedConfig16+

Sets the key event consumption configuration.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Name Type Readable Writable Description
key number Yes No Key value.
Currently, only the KEYCODE_VOLUME_UP and KEYCODE_VOLUME_DOWN keys are supported.
action number Yes No Key event type. Currently, the value can only be 1.
- 1: Key press.
- 2: Key release.
isRepeat boolean Yes No Whether to report repeated key events.

inputConsumer.getAllSystemHotkeys14+

getAllSystemHotkeys(): Promise<Array<HotkeyOptions>>

Obtains all system shortcut keys. This API uses a promise to return the result.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Return value

Parameter Description
Promise<Array<HotkeyOptions>> Promise used to return the list of all system shortcut keys.

Error codes:

For details about the error codes, see Universal Error Codes.

Error Code Error Message
801 Capability not supported.

Example

inputConsumer.getAllSystemHotkeys().then((data: Array<inputConsumer.HotkeyOptions>) => {
  console.log(`List of system hotkeys : ${JSON.stringify(data)}`);
});

inputConsumer.on('hotkeyOptions')14+

on(type: 'hotkeyChange', hotkeyOptions: HotkeyOptions, callback: Callback<HotkeyOptions>): void

Enables listening for global combination key events. This API uses an asynchronous callback to return the combination key data when a combination key event that meets the specified condition occurs.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Parameters

Name Type Mandatory Description
type string Yes Event type. This parameter has a fixed value of hotkeyChange.
hotkeyOptions HotkeyOptions Yes Shortcut key options.
callback Callback<HotkeyOptions> Yes Callback used to return the combination key data when a global combination key event that meets the specified condition occurs.

Error codes:

For details about the error codes, see Input Consumer Error Codes and Universal Error Codes.

Error Code Error Message
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.
801 Capability not supported.
4200002 The hotkey has been used by the system.
4200003 The hotkey has been subscribed to by another.

Example

let leftCtrlKey = 2072;
let zKey = 2042;
let hotkeyOptions: inputConsumer.HotkeyOptions = {
  preKeys: [ leftCtrlKey ],
  finalKey: zKey,
  isRepeat: true
};
let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => {
  console.log(`hotkeyOptions: ${JSON.stringify(hotkeyOptions)}`);
}
try {
  inputConsumer.on("hotkeyChange", hotkeyOptions, hotkeyCallback);
} catch (error) {
  console.log(`Subscribe failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputConsumer.off('hotkeyOptions')14+

off(type: 'hotkeyChange', hotkeyOptions: HotkeyOptions, callback?: Callback<HotkeyOptions>): void

Disables listening for global combination key events.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Parameters

Name Type Mandatory Description
type string Yes Event type. This parameter has a fixed value of hotkeyChange.
hotkeyOptions HotkeyOptions Yes Shortcut key options.
callback Callback<HotkeyOptions> No Callback to unregister. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes:

For details about the error codes, see Universal Error Codes.

Error Code Error Message
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.
801 Capability not supported.

Example

let leftCtrlKey = 2072;
let zKey = 2042;
// Disable listening for a single callback.
let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => {
  console.log(`hotkeyOptions: ${JSON.stringify(hotkeyOptions)}`);
}
let hotkeyOption: inputConsumer.HotkeyOptions = {preKeys: [leftCtrlKey], finalKey: zKey, isRepeat: true};
try {
  inputConsumer.on("hotkeyChange", hotkeyOption, hotkeyCallback);
  inputConsumer.off("hotkeyChange", hotkeyOption, hotkeyCallback);
  console.log(`Unsubscribe success`);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
let leftCtrlKey = 2072;
let zKey = 2042;
// Disable listening for all callbacks.
let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => {
  console.log(`hotkeyOptions: ${JSON.stringify(hotkeyOptions)}`);
}
let hotkeyOption: inputConsumer.HotkeyOptions = {preKeys: [leftCtrlKey], finalKey: zKey, isRepeat: true};
try {
  inputConsumer.on("hotkeyChange", hotkeyOption, hotkeyCallback);
  inputConsumer.off("hotkeyChange", hotkeyOption);
  console.log(`Unsubscribe success`);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputConsumer.on('keyPressed')16+

on(type: 'keyPressed', options: KeyPressedConfig, callback: Callback<KeyEvent>): void

Subscribes to key press events. If the current application is in the foreground focus window, a callback is triggered when the specified key is pressed.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Parameters

Name Type Mandatory Description
type string Yes Event type. This parameter has a fixed value of keyPressed.
options KeyPressedConfig Yes Sets the key event consumption configuration.
callback Callback<KeyEvent> Yes Callback used to return the key event.

Error codes:

For details about the error codes, see Universal Error Codes.

Error Code Error Message
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.
801 Capability not supported.

Example

try {
  let options: inputConsumer.KeyPressedConfig = {
    key: 16,
    action: 1,
    isRepeat: false,
  }
  inputConsumer.on('keyPressed', options, (event: KeyEvent) => {
    console.log(`Subscribe success ${JSON.stringify(event)}`);
  });
} catch (error) {
  console.log(`Subscribe execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputConsumer.off('keyPressed')16+

off(type: 'keyPressed', callback?: Callback<KeyEvent>): void

Unsubscribes from key press events.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Parameters

Name Type Mandatory Description
type string Yes Event type. This parameter has a fixed value of keyPressed.
callback Callback<KeyEvent> No Callback to unregister. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes:

For details about the error codes, see Universal Error Codes.

Error Code Error Message
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.
801 Capability not supported.

Example

try {
  // Disable listening for a single callback.
  inputConsumer.off('keyPressed', (event: KeyEvent) => {
    console.log(`Unsubscribe success ${JSON.stringify(event)}`);
  });
  // Disable listening for all callbacks.
  inputConsumer.off("keyPressed");
} catch (error) {
  console.log(`Unsubscribe execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
其他
1
https://gitee.com/openharmony/docs.git
git@gitee.com:openharmony/docs.git
openharmony
docs
docs
OpenHarmony-5.1.0-Release

搜索帮助