• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputConsumer (Global Shortcut Keys) (System API)
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 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> - The APIs provided by this module are system APIs.
10>
11> - The APIs provided by this module apply only to system shortcut keys, which are global shortcut keys defined by the system.
12
13
14## Modules to Import
15
16```js
17import { inputConsumer } from '@kit.InputKit';
18```
19
20## inputConsumer.on
21
22on(type: 'key', keyOptions: KeyOptions, callback: Callback<KeyOptions>): void
23
24Enables 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.
25
26**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer
27
28**Parameters**
29
30| Name        | Type                        | Mandatory  | Description                                      |
31| ---------- | -------------------------- | ---- | ---------------------------------------- |
32| type       | string                     | Yes   | Event type. Currently, only **key** is supported.                      |
33| keyOptions | [KeyOptions](#keyoptions)  | Yes   | Combination key options.                |
34| callback   | Callback<KeyOptions> | Yes   | Callback used to return the combination key data when a combination key event that meets the specified condition occurs.|
35
36**Example**
37
38```js
39let leftAltKey = 2045;
40let tabKey = 2049;
41let keyOptions: inputConsumer.KeyOptions = {
42  preKeys: [ leftAltKey ],
43  finalKey: tabKey,
44  isFinalKeyDown: true,
45  finalKeyDownDuration: 0
46};
47let callback = (keyOptions: inputConsumer.KeyOptions) => {
48  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
49}
50try {
51  inputConsumer.on("key", keyOptions, callback);
52} catch (error) {
53  console.error(`Subscribe failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
54}
55```
56
57
58## inputConsumer.off
59
60off(type: 'key', keyOptions: KeyOptions, callback?: Callback<KeyOptions>): void
61
62Disables listening for combination key events.
63
64**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer
65
66**Parameters**
67
68| Name        | Type                        | Mandatory  | Description                             |
69| ---------- | -------------------------- | ---- | ------------------------------- |
70| type       | string                     | Yes   | Event type. Currently, only **key** is supported.             |
71| keyOptions | [KeyOptions](#keyoptions)  | Yes   | Combination key options.            |
72| 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.|
73
74**Example**
75
76```js
77let leftAltKey = 2045;
78let tabKey = 2049;
79// Disable listening for a single callback.
80let callback = (keyOptions: inputConsumer.KeyOptions) => {
81  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
82}
83let keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
84try {
85  inputConsumer.on("key", keyOption, callback);
86  inputConsumer.off("key", keyOption, callback);
87  console.log(`Unsubscribe success`);
88} catch (error) {
89  console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
90}
91```
92```js
93let leftAltKey = 2045;
94let tabKey = 2049;
95// Disable listening for all callbacks.
96let callback = (keyOptions: inputConsumer.KeyOptions) => {
97  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
98}
99let keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
100try {
101  inputConsumer.on("key", keyOption, callback);
102  inputConsumer.off("key", keyOption);
103  console.log(`Unsubscribe success`);
104} catch (error) {
105  console.error(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
106}
107```
108
109## inputConsumer.setShieldStatus<sup>11+</sup>
110
111setShieldStatus(shieldMode: ShieldMode, isShield: boolean): void
112
113Sets the shortcut key shield status.
114
115**Required permissions**: ohos.permission.INPUT_CONTROL_DISPATCHING
116
117**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer
118
119**Parameters**
120
121| Name        | Type                        | Mandatory  | Description                                      |
122| ---------- | -------------------------- | ---- | ---------------------------------------- |
123| shieldMode       | [ShieldMode](js-apis-inputconsumer-sys.md#shieldmode11)                     | Yes   | Shortcut key shield mode. Currently, only **FACTORY_MODE** is supported, which means to shield all shortcut keys.                      |
124| isShield | boolean  | Yes   | Whether to enable shortcut key shielding. The value **true** means to enable shortcut key shielding, and the value **false** indicates the opposite.             |
125
126**Example**
127
128```js
129let FACTORY_MODE = 0;
130try {
131  inputConsumer.setShieldStatus(FACTORY_MODE,true);
132  console.log(`set shield status success`);
133} catch (error) {
134  console.error(`set shield status failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
135}
136```
137
138## inputConsumer.getShieldStatus<sup>11+</sup>
139
140getShieldStatus(shieldMode: ShieldMode): boolean
141
142Obtains the shortcut key shield status.
143
144**Required permissions**: ohos.permission.INPUT_CONTROL_DISPATCHING
145
146**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer
147
148**Parameters**
149
150| Name        | Type                        | Mandatory  | Description                                      |
151| ---------- | -------------------------- | ---- | ---------------------------------------- |
152| shieldMode       | [ShieldMode](js-apis-inputconsumer-sys.md#shieldmode11)                    | Yes   | Shortcut key shield mode. Currently, only **FACTORY_MODE** is supported, which means to shield all shortcut keys.                      |
153
154**Return value**
155
156| Type        |  Description                                      |
157| ---------- |  ---------------------------------------- |
158| boolean                    | Whether to enable shortcut key shielding. The value **true** means to enable shortcut key shielding, and the value **false** indicates the opposite.                      |
159
160**Example**
161
162```js
163try {
164  let FACTORY_MODE = 0;
165  let shieldstatusResult:Boolean =  inputConsumer.getShieldStatus(FACTORY_MODE);
166  console.log(` get shield status result:${JSON.stringify(shieldstatusResult)}`);
167} catch (error) {
168  console.error(`Failed to get shield status, error: ${JSON.stringify(error, [`code`, `message`])}`);
169}
170```
171
172## KeyOptions
173
174Represents combination key options.
175
176**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer
177
178| Name       | Type  | Readable  | Writable  | Description     |
179| --------- | ------ | ---- | ---- | ------- |
180| 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.<br>For example, in the combination keys **Ctrl+Alt+A**, **Ctrl+Alt** are called preceding keys.|
181| finalKey             | number  | Yes   |  No| Final key. This parameter is mandatory. A callback is triggered by the final key.<br>For example, in the combination keys **Ctrl+Alt+A**, **A** is called the final key.|
182| isFinalKeyDown       | boolean | Yes   |  No| Whether the final key is pressed.<br>The value **true** indicates that the key is pressed, and the value **false** indicates the opposite.|
183| finalKeyDownDuration | number  | Yes   |  No| Duration for holding down the key, in μs.<br>If the value of this field is **0**, a callback is triggered immediately.<br>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.  |
184| isRepeat<sup>18+</sup> | 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**.|
185
186## shieldMode<sup>11+</sup>
187
188Enumerates shortcut key shield modes.
189
190**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer
191
192| Name                       | Value| Description          |
193| ------------------------------ | ----------- | ---------------- |
194| UNSET_MODE | -1 | Unspecified shield mode, which means not to shield shortcut keys.|
195| FACTORY_MODE | 0 | Factory mode, which means to shield all shortcut keys.|
196| OOBE_MODE | 1 | OOBE mode, which means to shield all shortcut keys during the OOBE. This function is not supported currently.|
197