js-apis-inputconsumer.md 4.9 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.multimodalInput.inputConsumer (Input Consumer)
Z
zengyawen 已提交
2

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

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


## Modules to Import


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


## inputConsumer.on

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

S
shawn_he 已提交
22
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 已提交
23

Z
zengyawen 已提交
24 25
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

S
shawn_he 已提交
26 27
**Parameters**

S
shawn_he 已提交
28 29 30 31 32
| 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 已提交
33

S
shawn_he 已提交
34
**Example**
Z
zengyawen 已提交
35

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


## inputConsumer.off

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

S
shawn_he 已提交
53
Disables listening for combination key events.
S
shawn_he 已提交
54

Z
zengyawen 已提交
55 56
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

S
shawn_he 已提交
57 58
**Parameters**

S
shawn_he 已提交
59 60 61 62 63
| 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 已提交
64

S
shawn_he 已提交
65
**Example**
Z
zengyawen 已提交
66

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


S
shawn_he 已提交
101
## KeyOptions
Z
zengyawen 已提交
102

S
shawn_he 已提交
103
Represents combination key options.
S
shawn_he 已提交
104

S
shawn_he 已提交
105 106
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

S
shawn_he 已提交
107 108
| Name       | Type  | Readable  | Writable  | Description     |
| --------- | ------ | ---- | ---- | ------- |
S
shawn_he 已提交
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.|
S
shawn_he 已提交
110 111 112
| 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.  |