• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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
12## Modules to Import
13
14
15```js
16import inputConsumer from '@ohos.multimodalInput.inputConsumer';
17```
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.log(`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 for which listening is disabled. 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.log(`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.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
106}
107```
108
109
110## KeyOptions
111
112Represents combination key options.
113
114**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer
115
116| Name       | Type  | Readable  | Writable  | Description     |
117| --------- | ------ | ---- | ---- | ------- |
118| 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.|
119| finalKey             | number  | Yes   |  No| Final key. This parameter is mandatory. A callback is triggered by the final key.|
120| isFinalKeyDown       | boolean | Yes   |  No| Whether the final key is pressed.|
121| 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.  |
122