1# @ohos.multimodalInput.inputConsumer (Input Consumer) 2 3The **inputConsumer** module implements listening for combination key events. 4 5> **NOTE** 6> - 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. 7> - The APIs provided by this module are system APIs. 8 9 10## Modules to Import 11 12 13```js 14import inputConsumer from '@ohos.multimodalInput.inputConsumer'; 15``` 16 17 18## inputConsumer.on 19 20on(type: "key", keyOptions: KeyOptions, callback: Callback<KeyOptions>): void 21 22Enables 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. 23 24**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 25 26**Parameters** 27 28| Name | Type | Mandatory | Description | 29| ---------- | -------------------------- | ---- | ---------------------------------------- | 30| type | string | Yes | Event type. Currently, only **key** is supported. | 31| keyOptions | [KeyOptions](#keyoptions) | Yes | Combination key options. | 32| callback | Callback<KeyOptions> | Yes | Callback used to return the combination key data when a combination key event that meets the specified condition occurs.| 33 34**Example** 35 36```js 37let leftAltKey = 2045; 38let tabKey = 2049; 39try { 40 inputConsumer.on("key", {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0}, keyOptions => { 41 console.log(`keyOptions: ${JSON.stringify(keyOptions)}`); 42 }); 43} catch (error) { 44 console.log(`Subscribe failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 45} 46``` 47 48 49## inputConsumer.off 50 51off(type: "key", keyOptions: KeyOptions, callback?: Callback<KeyOptions>): void 52 53Disables listening for combination key events. 54 55**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 56 57**Parameters** 58 59| Name | Type | Mandatory | Description | 60| ---------- | -------------------------- | ---- | ------------------------------- | 61| type | string | Yes | Event type. Currently, only **key** is supported. | 62| keyOptions | [KeyOptions](#keyoptions) | Yes | Combination key options. | 63| callback | Callback<KeyOptions> | No | Callback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.| 64 65**Example** 66 67```js 68let leftAltKey = 2045; 69let tabKey = 2049; 70// Disable listening for a single callback function. 71let callback = function (keyOptions) { 72 console.log(`keyOptions: ${JSON.stringify(keyOptions)}`); 73} 74let keyOption = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0}; 75try { 76 inputConsumer.on("key", keyOption, callback); 77 inputConsumer.off("key", keyOption, callback); 78 console.log(`Unsubscribe success`); 79} catch (error) { 80 console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 81} 82``` 83```js 84let leftAltKey = 2045; 85let tabKey = 2049; 86// Disable listening for all callback functions. 87let callback = function (keyOptions) { 88 console.log(`keyOptions: ${JSON.stringify(keyOptions)}`); 89} 90let keyOption = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0}; 91try { 92 inputConsumer.on("key", keyOption, callback); 93 inputConsumer.off("key", keyOption); 94 console.log(`Unsubscribe success`); 95} catch (error) { 96 console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 97} 98``` 99 100 101## KeyOptions 102 103Represents combination key options. 104 105**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer 106 107| Name | Type | Readable | Writable | Description | 108| --------- | ------ | ---- | ---- | ------- | 109| preKeys | Array\<number> | Yes | No| Front key set. The number of front keys ranges from 0 to 4. There is no requirement on the sequence of the keys.| 110| finalKey | number | Yes | No| Final key. This parameter is mandatory. A callback function is triggered by the final key.| 111| isFinalKeyDown | boolean | Yes | No| Whether the final key is pressed.| 112| finalKeyDownDuration | number | Yes | No| Duration within which the final key is pressed. If the value is **0**, the callback function is triggered immediately. If the value is greater than **0** and the value of **isFinalKeyDown** is **true**, the callback function is triggered when the key press duration is longer than the value of this parameter. If the value of **isFinalKeyDown** is **false**, the callback function is triggered when the duration from key press to key release is less than the value of this parameter. | 113