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-sys](../../reference/apis-input-kit/js-apis-inputconsumer-sys.md) and [ohos.multimodalInput.inputConsumer](../../reference/apis-input-kit/js-apis-inputconsumer.md).
16
17| API | Description|
18| ------------------------------------------------------------ | -------------------------- |
19| on(type: 'key', keyOptions: KeyOptions, callback: Callback\<KeyOptions>): void | Enables listening for combination key events.|
20| off(type: 'key', keyOptions: KeyOptions, callback?: Callback\<KeyOptions>): void | Disables 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| getAllSystemHotkeys(): Promise\<Array\<HotkeyOptions>> | Obtains all system combination keys.|
24| on(type: 'hotkeyChange', hotkeyOptions: HotkeyOptions, callback: Callback\<HotkeyOptions>): void | Enables listening for global combination key events.|
25| off(type: 'hotkeyChange', hotkeyOptions: HotkeyOptions, callback?: Callback\<HotkeyOptions>): void | Disables listening for global combination key events.|
26
27## How to Develop
28
29When 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.
30When the application is stopped, [off](../../reference/apis-input-kit/js-apis-inputconsumer-sys.md#inputconsumeroff) is called to unsubscribe from combination key events.
31
32```js
33let leftAltKey = 2045;
34let tabKey = 2049;
35let callback = (keyOptions: inputConsumer.KeyOptions) => {
36  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
37}
38// Start the application.
39let keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
40try {
41  inputConsumer.on("key", keyOption, callback);// Listen for combination key events.
42} catch (error) {
43  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
44}
45// Stop the application.
46try {
47  inputConsumer.off("key", keyOption, callback);// Disable listening for combination key events.
48  console.log(`Unsubscribe success`);
49} catch (error) {
50  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
51}
52
53let leftCtrlKey = 2072;
54let zKey = 2042;
55let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => {
56  console.log(`keyOptions: ${JSON.stringify(hotkeyOptions)}`);
57}
58let hotkeyOption: inputConsumer.HotkeyOptions = {preKeys: [leftCtrlKey], finalKey: zKey, isRepeat: false};
59// 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.
60inputConsumer.getAllSystemHotkeys().then((data: Array<inputConsumer.HotkeyOptions>) => {//: Obtain all system combination keys.
61  console.log(`List of system hotkeys : ${JSON.stringify(data)}`);
62});
63// Start the application.
64try {
65  inputConsumer.on("hotkeyChange", hotkeyOption, hotkeyCallback);// Subscribe to global combination keys.
66} catch (error) {
67  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
68}
69// Stop the application.
70try {
71  inputConsumer.off("hotkeyChange", hotkeyOption, hotkeyCallback);// Unsubscribe from global combination keys.
72  console.log(`Unsubscribe success`);
73} catch (error) {
74  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
75}
76```
77