js-apis-inputconsumer.md 4.8 KB
Newer Older
S
shawn_he 已提交
1
# Input Consumer
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
The Input Consumer module implements listening for combination key events.
Z
zengyawen 已提交
4

S
shawn_he 已提交
5
> **NOTE**
S
shawn_he 已提交
6
>
Z
zengyawen 已提交
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.
S
shawn_he 已提交
8
>
S
shawn_he 已提交
9
> - The APIs provided by this module are system APIs.
Z
zengyawen 已提交
10 11 12 13 14


## Modules to Import


S
shawn_he 已提交
15
```js
Z
zengyawen 已提交
16 17 18 19 20 21
import inputConsumer from '@ohos.multimodalInput.inputConsumer';
```


## inputConsumer.on

S
shawn_he 已提交
22
on(type: "key", keyOptions: KeyOptions, callback: Callback<KeyOptions>): void
Z
zengyawen 已提交
23

S
shawn_he 已提交
24
Enables 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.
S
shawn_he 已提交
25

Z
zengyawen 已提交
26 27
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

S
shawn_he 已提交
28 29
**Parameters**

S
shawn_he 已提交
30 31 32 33 34
| Name        | Type                        | Mandatory  | Description                                      |
| ---------- | -------------------------- | ---- | ---------------------------------------- |
| type       | string                     | Yes   | Event type. Currently, only **key** is supported.                      |
| keyOptions | [KeyOptions](#keyoptions)  | Yes   | Combination key options.                |
| callback   | Callback<KeyOptions> | Yes   | Callback used to return the combination key data when a combination key event that meets the specified condition occurs.|
Z
zengyawen 已提交
35

S
shawn_he 已提交
36
**Example**
Z
zengyawen 已提交
37

S
shawn_he 已提交
38 39 40
```js
let leftAltKey = 2045;
let tabKey = 2049;
S
shawn_he 已提交
41
try {
S
shawn_he 已提交
42 43 44
  inputConsumer.on("key", {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0}, keyOptions => {
    console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
  });
S
shawn_he 已提交
45
} catch (error) {
S
shawn_he 已提交
46
  console.log(`Subscribe failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
Z
zengyawen 已提交
47 48 49 50 51 52
}
```


## inputConsumer.off

S
shawn_he 已提交
53
off(type: "key", keyOptions: KeyOptions, callback?: Callback<KeyOptions>): void
Z
zengyawen 已提交
54

S
shawn_he 已提交
55
Disables listening for combination key events.
S
shawn_he 已提交
56

Z
zengyawen 已提交
57 58
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

S
shawn_he 已提交
59 60
**Parameters**

S
shawn_he 已提交
61 62 63 64 65
| Name        | Type                        | Mandatory  | Description                             |
| ---------- | -------------------------- | ---- | ------------------------------- |
| type       | string                     | Yes   | Event type. Currently, only **key** is supported.             |
| keyOptions | [KeyOptions](#keyoptions)  | Yes   | Combination key options.            |
| 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.|
Z
zengyawen 已提交
66

S
shawn_he 已提交
67
**Example**
Z
zengyawen 已提交
68

S
shawn_he 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
```js
let leftAltKey = 2045;
let tabKey = 2049;
// Disable listening for a single callback function.
let callback = function (keyOptions) {
  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
}
let keyOption = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
try {
  inputConsumer.on("key", keyOption, callback);
  inputConsumer.off("key", keyOption, callback);
  console.log(`Unsubscribe success`);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
Z
zengyawen 已提交
84
```
S
shawn_he 已提交
85 86 87 88
```js
let leftAltKey = 2045;
let tabKey = 2049;
// Disable listening for all callback functions.
S
shawn_he 已提交
89
let callback = function (keyOptions) {
S
shawn_he 已提交
90
  console.log(`keyOptions: ${JSON.stringify(keyOptions)}`);
S
shawn_he 已提交
91
}
S
shawn_he 已提交
92
let keyOption = {preKeys: [leftAltKey], finalKey: tabKey, isFinalKeyDown: true, finalKeyDownDuration: 0};
S
shawn_he 已提交
93
try {
S
shawn_he 已提交
94 95 96
  inputConsumer.on("key", keyOption, callback);
  inputConsumer.off("key", keyOption);
  console.log(`Unsubscribe success`);
S
shawn_he 已提交
97
} catch (error) {
S
shawn_he 已提交
98
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
Z
zengyawen 已提交
99 100 101 102
}
```


S
shawn_he 已提交
103
## KeyOptions
Z
zengyawen 已提交
104

S
shawn_he 已提交
105
Represents combination key options.
S
shawn_he 已提交
106

S
shawn_he 已提交
107 108
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

S
shawn_he 已提交
109 110 111 112 113 114
| Name       | Type  | Readable  | Writable  | Description     |
| --------- | ------ | ---- | ---- | ------- |
| 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.|
| finalKey             | number  | Yes   |  No| Final key. This parameter is mandatory. A callback function is triggered by the final key.|
| isFinalKeyDown       | boolean | Yes   |  No| Whether the final key is pressed.|
| 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.  |