• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Combination Key Development
2
3## When to Use
4
5The **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.
6
7## Modules to Import
8
9```js
10import { inputConsumer } from '@kit.InputKit';
11```
12
13## Available APIs
14
15The following table lists the common APIs provided by the **inputConsumer** module. For details, see [ohos.multimodalInput.inputConsumer](../../reference/apis-input-kit/js-apis-inputconsumer-sys.md).
16
17| API | Description|
18| ------------------------------------------------------------ | -------------------------- |
19| on(type: 'key', keyOptions: KeyOptions, callback: Callback<KeyOptions>): void | Listens for combination key events.|
20| off(type: 'key', keyOptions: KeyOptions, callback?: Callback<KeyOptions>): void | Cancels listening for combination key events.|
21| setShieldStatus(shieldMode: ShieldMode, isShield: boolean): void | Sets the key shielding status.|
22| getShieldStatus(shieldMode: ShieldMode): boolean | Checks whether key shielding is enabled.|
23
24## How to Develop
25
26When an application that uses specific combination keys is started, [on](../../reference/apis-input-kit/js-apis-inputconsumer-sys.md#inputconsumeron) is called to subscribe to combination key events.
27When the application is stopped, [off](../../reference/apis-input-kit/js-apis-inputconsumer-sys.md#inputconsumeroff) is called to unsubscribe from combination key events.
28
29```js
30let leftAltKey = 2045;
31let tabKey = 2049;
32let callback = (keyOptions: inputConsumer.KeyOptions) => {
33  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
34}
35// Start the application.
36let keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
37try {
38  inputConsumer.on("key", keyOption, callback);// Listen for combination key events.
39} catch (error) {
40  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
41}
42// Stop the application.
43try {
44  inputConsumer.off("key", keyOption, callback);// Cancel listening for combination key events.
45  console.log(`Unsubscribe success`);
46} catch (error) {
47  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
48}
49```
50