1# @ohos.multimodalInput.inputConsumer (Input Consumer) 2 3The **inputConsumer** module implements listening for combination key events. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 13. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9 10## Modules to Import 11 12 13```js 14import { inputConsumer } from '@kit.InputKit'; 15``` 16 17## HotkeyOptions<sup>13+</sup> 18 19Defines shortcut key options. 20 21**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 22 23| Name | Type | Readable | Writable | Description | 24| --------- | ------ | ------- | ------- | ------- | 25| preKeys | Array<number> | Yes | No | Modifier key set. The number of modifier keys is [1, 2]. There is no requirement on the sequence of modifier keys.<br>For example, in **Ctrl+Shift+Esc**, **Ctrl** and **Shift** are modifier keys.| 26| finalKey | number | Yes | No | Modified key.<br>For example, in **Ctrl+Shift+Esc**, **Esc** is the modified key.| 27| 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**.| 28 29## inputConsumer.getAllSystemHotkeys<sup>13+</sup> 30 31getAllSystemHotkeys(): Promise<Array<HotkeyOptions>> 32 33Obtains all system shortcut keys. This API uses a promise to return the result. 34 35**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 36 37**Return value** 38 39| Parameter | Description | 40| ---------- | ---------------------------------------- | 41| Promise<Array<HotkeyOptions>> | Promise used to return the list of all system shortcut keys.| 42 43**Example** 44 45```js 46inputConsumer.getAllSystemHotkeys().then((data: Array<inputConsumer.HotkeyOptions>) => { 47 console.log(`List of system hotkeys : ${JSON.stringify(data)}`); 48}); 49``` 50 51## inputConsumer.on<sup>13+</sup> 52 53on(type: 'hotkeyChange', hotkeyOptions: HotkeyOptions, callback: Callback<HotkeyOptions>): void 54 55Enables 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. 56 57**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 58 59**Parameters** 60 61| Name | Type | Mandatory | Description | 62| ---------- | -------------------------- | ---- | ---------- | 63| type | string | Yes | Event type. This parameter has a fixed value of **hotkeyChange**. | 64| hotkeyOptions | [HotkeyOptions](#hotkeyoptions13) | Yes | Shortcut key options. | 65| callback | Callback<HotkeyOptions> | Yes | Callback used to return the combination key data when a global combination key event that meets the specified condition occurs.| 66 67**Example** 68 69```js 70let leftCtrlKey = 2072; 71let zKey = 2042; 72let hotkeyOptions: inputConsumer.HotkeyOptions = { 73 preKeys: [ leftCtrlKey ], 74 finalKey: zKey, 75 isRepeat: true 76}; 77let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => { 78 console.log(`hotkeyOptions: ${JSON.stringify(hotkeyOptions)}`); 79} 80try { 81 inputConsumer.on("hotkeyChange", hotkeyOptions, hotkeyCallback); 82} catch (error) { 83 console.log(`Subscribe failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 84} 85``` 86 87## inputConsumer.off<sup>13+</sup> 88 89off(type: 'hotkeyChange', hotkeyOptions: HotkeyOptions, callback?: Callback<HotkeyOptions>): void 90 91Disables listening for global combination key events. 92 93**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 94 95**Parameters** 96 97| Name | Type | Mandatory | Description | 98| ---------- | -------------------------- | ---- | ---------- | 99| type | string | Yes | Event type. This parameter has a fixed value of **hotkeyChange**. | 100| hotkeyOptions | [HotkeyOptions](#hotkeyoptions13) | Yes | Shortcut key options. | 101| 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.| 102 103**Example** 104 105```js 106let leftCtrlKey = 2072; 107let zKey = 2042; 108// Disable listening for a single callback. 109let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => { 110 console.log(`hotkeyOptions: ${JSON.stringify(hotkeyOptions)}`); 111} 112let hotkeyOption: inputConsumer.HotkeyOptions = {preKeys: [leftCtrlKey], finalKey: zKey, isRepeat: true}; 113try { 114 inputConsumer.on("hotkeyChange", hotkeyOption, hotkeyCallback); 115 inputConsumer.off("hotkeyChange", hotkeyOption, hotkeyCallback); 116 console.log(`Unsubscribe success`); 117} catch (error) { 118 console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 119} 120``` 121```js 122let leftCtrlKey = 2072; 123let zKey = 2042; 124// Disable listening for all callbacks. 125let hotkeyCallback = (hotkeyOptions: inputConsumer.HotkeyOptions) => { 126 console.log(`hotkeyOptions: ${JSON.stringify(hotkeyOptions)}`); 127} 128let hotkeyOption: inputConsumer.HotkeyOptions = {preKeys: [leftCtrlKey], finalKey: zKey, isRepeat: true}; 129try { 130 inputConsumer.on("hotkeyChange", hotkeyOption, hotkeyCallback); 131 inputConsumer.off("hotkeyChange", hotkeyOption); 132 console.log(`Unsubscribe success`); 133} catch (error) { 134 console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 135} 136``` 137