harmony 鸿蒙@ohos.multimodalInput.inputConsumer (Input Consumer)

  • 2022-08-09
  • 浏览 (1002)

@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 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs provided by this module are system APIs.

Modules to Import

import inputConsumer from '@ohos.multimodalInput.inputConsumer';

inputConsumer.on

on(type: ‘key’, keyOptions: KeyOptions, callback: Callback<KeyOptions>): void

Enables listening for 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. Currently, only key is supported.
keyOptions KeyOptions Yes Combination key options.
callback Callback<KeyOptions> Yes Callback used to return the combination key data when a combination key event that meets the specified condition occurs.

Example

let leftAltKey = 2045;
let tabKey = 2049;
let keyOptions: inputConsumer.KeyOptions = {
  preKeys: [ leftAltKey ],
  finalKey: tabKey,
  isFinalKeyDown: true,
  finalKeyDownDuration: 0
};
let callback = (keyOptions: inputConsumer.KeyOptions) => {
  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
}
try {
  inputConsumer.on("key", keyOptions, callback);
} catch (error) {
  console.log(`Subscribe failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputConsumer.off

off(type: ‘key’, keyOptions: KeyOptions, callback?: Callback<KeyOptions>): void

Disables listening for combination key events.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Parameters

Name Type Mandatory Description
type string Yes Event type. Currently, only key is supported.
keyOptions KeyOptions Yes Combination key options.
callback Callback<KeyOptions> No Callback to unregister. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Example

let leftAltKey = 2045;
let tabKey = 2049;
// Disable listening for a single callback.
let callback = (keyOptions: inputConsumer.KeyOptions) => {
  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
}
let keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
try {
  inputConsumer.on("key", keyOption, callback);
  inputConsumer.off("key", keyOption, callback);
  console.log(`Unsubscribe success`);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
let leftAltKey = 2045;
let tabKey = 2049;
// Disable listening for all callbacks.
let callback = (keyOptions: inputConsumer.KeyOptions) => {
  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
}
let keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
try {
  inputConsumer.on("key", keyOption, callback);
  inputConsumer.off("key", keyOption);
  console.log(`Unsubscribe success`);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

KeyOptions

Represents combination key options.

System capability: SystemCapability.MultimodalInput.Input.InputConsumer

Name Type Readable Writable Description
preKeys Array<number> Yes No Preceding key set. The number of preceding keys ranges from 0 to 4. There is no requirement on the sequence of the keys.
For example, in the combination keys Ctrl+Alt+A, Ctrl+Alt are called preceding keys.
finalKey number Yes No Final key. This parameter is mandatory. A callback is triggered by the final key.
For example, in the combination keys Ctrl+Alt+A, A is called the final key.
isFinalKeyDown boolean Yes No Whether the final key is pressed.
The value true indicates that the key is pressed, and the value false indicates the opposite.
finalKeyDownDuration number Yes No Duration for pressing a key, in μs.
If the value of this field is 0, a callback is triggered immediately.
If the value of this field is greater than 0 and isFinalKeyDown is true, a callback is triggered when the key keeps being pressed after the specified duration expires. If isFinalKeyDown is false, a callback is triggered when the key is released before the specified duration expires.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞