js-apis-inputconsumer.md 5.0 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**
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

## Modules to Import

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

## inputConsumer.on

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

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

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

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

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

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

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


## inputConsumer.off

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

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

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

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

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

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

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

S
shawn_he 已提交
99
## KeyOptions
Z
zengyawen 已提交
100

S
shawn_he 已提交
101
Represents combination key options.
S
shawn_he 已提交
102

S
shawn_he 已提交
103 104
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

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