1e41f4b71Sopenharmony_ci# @ohos.multimodalInput.inputConsumer-sys (Input Consumer) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **inputConsumer** module implements listening for combination key events. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> - 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. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> - The APIs provided by this module are system APIs. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Modules to Import 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```js 14e41f4b71Sopenharmony_ciimport { inputConsumer } from '@kit.InputKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## inputConsumer.on 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_cion(type: 'key', keyOptions: KeyOptions, callback: Callback<KeyOptions>): void 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciEnables 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. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**Parameters** 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 28e41f4b71Sopenharmony_ci| ---------- | -------------------------- | ---- | ---------------------------------------- | 29e41f4b71Sopenharmony_ci| type | string | Yes | Event type. Currently, only **key** is supported. | 30e41f4b71Sopenharmony_ci| keyOptions | [KeyOptions](#keyoptions) | Yes | Combination key options. | 31e41f4b71Sopenharmony_ci| callback | Callback<KeyOptions> | Yes | Callback used to return the combination key data when a combination key event that meets the specified condition occurs.| 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**Example** 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci```js 36e41f4b71Sopenharmony_cilet leftAltKey = 2045; 37e41f4b71Sopenharmony_cilet tabKey = 2049; 38e41f4b71Sopenharmony_cilet keyOptions: inputConsumer.KeyOptions = { 39e41f4b71Sopenharmony_ci preKeys: [ leftAltKey ], 40e41f4b71Sopenharmony_ci finalKey: tabKey, 41e41f4b71Sopenharmony_ci isFinalKeyDown: true, 42e41f4b71Sopenharmony_ci finalKeyDownDuration: 0 43e41f4b71Sopenharmony_ci}; 44e41f4b71Sopenharmony_cilet callback = (keyOptions: inputConsumer.KeyOptions) => { 45e41f4b71Sopenharmony_ci console.log(`keyOptions: ${JSON.stringify(keyOptions)}`); 46e41f4b71Sopenharmony_ci} 47e41f4b71Sopenharmony_citry { 48e41f4b71Sopenharmony_ci inputConsumer.on("key", keyOptions, callback); 49e41f4b71Sopenharmony_ci} catch (error) { 50e41f4b71Sopenharmony_ci console.log(`Subscribe failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 51e41f4b71Sopenharmony_ci} 52e41f4b71Sopenharmony_ci``` 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci## inputConsumer.off 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_cioff(type: 'key', keyOptions: KeyOptions, callback?: Callback<KeyOptions>): void 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciDisables listening for combination key events. 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**Parameters** 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 66e41f4b71Sopenharmony_ci| ---------- | -------------------------- | ---- | ------------------------------- | 67e41f4b71Sopenharmony_ci| type | string | Yes | Event type. Currently, only **key** is supported. | 68e41f4b71Sopenharmony_ci| keyOptions | [KeyOptions](#keyoptions) | Yes | Combination key options. | 69e41f4b71Sopenharmony_ci| 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.| 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**Example** 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci```js 74e41f4b71Sopenharmony_cilet leftAltKey = 2045; 75e41f4b71Sopenharmony_cilet tabKey = 2049; 76e41f4b71Sopenharmony_ci// Disable listening for a single callback. 77e41f4b71Sopenharmony_cilet callback = (keyOptions: inputConsumer.KeyOptions) => { 78e41f4b71Sopenharmony_ci console.log(`keyOptions: ${JSON.stringify(keyOptions)}`); 79e41f4b71Sopenharmony_ci} 80e41f4b71Sopenharmony_cilet keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0}; 81e41f4b71Sopenharmony_citry { 82e41f4b71Sopenharmony_ci inputConsumer.on("key", keyOption, callback); 83e41f4b71Sopenharmony_ci inputConsumer.off("key", keyOption, callback); 84e41f4b71Sopenharmony_ci console.log(`Unsubscribe success`); 85e41f4b71Sopenharmony_ci} catch (error) { 86e41f4b71Sopenharmony_ci console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 87e41f4b71Sopenharmony_ci} 88e41f4b71Sopenharmony_ci``` 89e41f4b71Sopenharmony_ci```js 90e41f4b71Sopenharmony_cilet leftAltKey = 2045; 91e41f4b71Sopenharmony_cilet tabKey = 2049; 92e41f4b71Sopenharmony_ci// Disable listening for all callbacks. 93e41f4b71Sopenharmony_cilet callback = (keyOptions: inputConsumer.KeyOptions) => { 94e41f4b71Sopenharmony_ci console.log(`keyOptions: ${JSON.stringify(keyOptions)}`); 95e41f4b71Sopenharmony_ci} 96e41f4b71Sopenharmony_cilet keyOption: inputConsumer.KeyOptions = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0}; 97e41f4b71Sopenharmony_citry { 98e41f4b71Sopenharmony_ci inputConsumer.on("key", keyOption, callback); 99e41f4b71Sopenharmony_ci inputConsumer.off("key", keyOption); 100e41f4b71Sopenharmony_ci console.log(`Unsubscribe success`); 101e41f4b71Sopenharmony_ci} catch (error) { 102e41f4b71Sopenharmony_ci console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 103e41f4b71Sopenharmony_ci} 104e41f4b71Sopenharmony_ci``` 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci## inputConsumer.setShieldStatus<sup>11+</sup> 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_cisetShieldStatus(shieldMode: ShieldMode, isShield: boolean): void 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ciSets the key shielding status. 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INPUT_CONTROL_DISPATCHING 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**Parameters** 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 119e41f4b71Sopenharmony_ci| ---------- | -------------------------- | ---- | ---------------------------------------- | 120e41f4b71Sopenharmony_ci| shieldMode | ShieldMode | Yes | Shielding mode. Currently, only **FACTORY_MODE** is supported. | 121e41f4b71Sopenharmony_ci| isShield | boolean | Yes | Whether to enable key shielding. The value **true** means to enable key shielding, and the value **false** indicates the opposite. | 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**Example** 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci```js 126e41f4b71Sopenharmony_cilet FACTORY_MODE = 0; 127e41f4b71Sopenharmony_citry { 128e41f4b71Sopenharmony_ci inputConsumer.setShieldStatus(FACTORY_MODE,true); 129e41f4b71Sopenharmony_ci console.log(`set shield status success`); 130e41f4b71Sopenharmony_ci} catch (error) { 131e41f4b71Sopenharmony_ci console.log(`set shield status failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 132e41f4b71Sopenharmony_ci} 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci``` 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci## inputConsumer.getShieldStatus<sup>11+</sup> 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_cigetShieldStatus(shieldMode: ShieldMode): boolean 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ciChecks whether key shielding is enabled. 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.INPUT_CONTROL_DISPATCHING 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci**Parameters** 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 149e41f4b71Sopenharmony_ci| ---------- | -------------------------- | ---- | ---------------------------------------- | 150e41f4b71Sopenharmony_ci| shieldMode | ShieldMode | Yes | Shielding mode. Currently, only **FACTORY_MODE** is supported. | 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**Return value** 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci| Parameter | Description | 155e41f4b71Sopenharmony_ci| ---------- | ---------------------------------------- | 156e41f4b71Sopenharmony_ci| boolean | Whether to enable key shielding. The value **true** means to enable key shielding, and the value **false** indicates the opposite. | 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci**Example** 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci```js 161e41f4b71Sopenharmony_citry { 162e41f4b71Sopenharmony_ci let FACTORY_MODE = 0; 163e41f4b71Sopenharmony_ci let shieldstatusResult:Boolean = inputConsumer.getShieldStatus(FACTORY_MODE); 164e41f4b71Sopenharmony_ci console.log(` get shield status result:${JSON.stringify(shieldstatusResult)}`); 165e41f4b71Sopenharmony_ci} catch (error) { 166e41f4b71Sopenharmony_ci console.log(`Failed to get shield status, error: ${JSON.stringify(error, [`code`, `message`])}`); 167e41f4b71Sopenharmony_ci} 168e41f4b71Sopenharmony_ci``` 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci## KeyOptions 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ciRepresents combination key options. 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 177e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---- | ------- | 178e41f4b71Sopenharmony_ci| 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.| 179e41f4b71Sopenharmony_ci| 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.| 180e41f4b71Sopenharmony_ci| 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.| 181e41f4b71Sopenharmony_ci| finalKeyDownDuration | number | Yes | No| Duration for pressing a 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. | 182e41f4b71Sopenharmony_ci| isRepeat<sup>13+</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**.| 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci## shieldMode<sup>11+</sup> 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ciEnumerates key shielding modes. 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 191e41f4b71Sopenharmony_ci| ------------------------------ | ----------- | ---------------- | ---------------- | ---------------- | 192e41f4b71Sopenharmony_ci| FACTORY_MODE | number | Yes| No| Factory mode, which means to shield all shortcut keys.| 193