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