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

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

S
shawn_he 已提交
5 6
> **NOTE**<br>
>
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
>
Z
zengyawen 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21
> - The APIs of this module are system APIs and cannot be called by third-party applications.


## Modules to Import


```
import inputConsumer from '@ohos.multimodalInput.inputConsumer';
```


## inputConsumer.on

S
shawn_he 已提交
22
on(type: "key", keyOptions: KeyOptions, callback: Callback&lt;KeyOptions&gt;): void
Z
zengyawen 已提交
23

S
shawn_he 已提交
24
Enables listening for combination key events. When a combination key event that meets the specified conditions occurs, **keyOptions** will be passed as an input parameter to **callback**.
Z
zengyawen 已提交
25

S
shawn_he 已提交
26 27
This is a system API.

Z
zengyawen 已提交
28 29
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

S
shawn_he 已提交
30 31 32
**Parameters**

| Name| Type| Mandatory| Description| 
Z
zengyawen 已提交
33
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
34 35 36
| type | string | Yes| Type of the key input event to listen for. Only **key** is supported.|
| keyOptions | [keyOptions](#keyOptions) | Yes| Key option, which specifies the condition for combination key input.|
| callback | Callback&lt;KeyOptions&gt; | Yes| Callback used to return the result.<br> When a key input event that meets the specified options occurs, **keyOptions** will be passed as an input parameter to **callback**.| 
Z
zengyawen 已提交
37

S
shawn_he 已提交
38
**Example**
Z
zengyawen 已提交
39 40

```
S
shawn_he 已提交
41 42 43 44
let keyOptions = {preKeys: [], finalKey: 3, isFinalKeyDown: true, finalKeyDownDuration: 0}
let callback = function(keyOptions) {
    console.info("preKeys: " + keyOptions.preKeys, "finalKey: " + keyOptions.finalKey, 
                 "isFinalKeyDown: " + keyOptions.isFinalKeyDown, "finalKeyDownDuration: " + keyOptions.finalKeyDownDuration)
Z
zengyawen 已提交
45
}
S
shawn_he 已提交
46
inputConsumer.on('key', keyOptions, callback);
Z
zengyawen 已提交
47 48 49 50 51
```


## inputConsumer.off

S
shawn_he 已提交
52
off(type: "key", keyOptions: KeyOptions, callback?: Callback&lt;KeyOptions&gt;): void
Z
zengyawen 已提交
53 54 55

Stops listening for combination key events.

S
shawn_he 已提交
56 57
This is a system API.

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

S
shawn_he 已提交
60 61 62
**Parameters**

| Name| Type| Mandatory| Description| 
Z
zengyawen 已提交
63
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
64 65
| type | string | Yes| Type of the key input event to listen for. Only **key** is supported.| 
| keyOptions | [keyOptions](#keyOptions) | Yes| Key options passed to the key input event when listening starts.| 
S
shawn_he 已提交
66
| callback | Callback&lt;KeyOptions&gt; | Yes| Callback function passed to the key input event with **keyOptions** when listening starts.| 
Z
zengyawen 已提交
67

S
shawn_he 已提交
68
**Example**
Z
zengyawen 已提交
69 70

```
S
shawn_he 已提交
71 72 73 74
let keyOptions = {preKeys: [], finalKey: 3, isFinalKeyDown: true, finalKeyDownDuration: 0}
let callback = function(keyOptions) {
    console.info("preKeys: " + keyOptions.preKeys, "finalKey: " + keyOptions.finalKey, 
                 "isFinalKeyDown: " + keyOptions.isFinalKeyDown, "finalKeyDownDuration: " + keyOptions.finalKeyDownDuration)
Z
zengyawen 已提交
75
}
S
shawn_he 已提交
76
inputConsumer.off('key', keyOptions, callback);
Z
zengyawen 已提交
77 78 79
```


S
shawn_he 已提交
80
## keyOptions
Z
zengyawen 已提交
81 82 83

Defines the key options that are met when a combination key input event occurs.

S
shawn_he 已提交
84 85
This is a system API.

S
shawn_he 已提交
86 87 88 89 90
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

**Parameters**

| Name| Type| Mandatory| Description| 
Z
zengyawen 已提交
91
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
92 93 94 95
| preKeys | Array | Yes| Array of precedent keys. This parameter can be left empty. There is no requirement on the sequence of precedent keys.| 
| finalKey | Number | Yes| Final key in the combination key. This parameter cannot be left blank.| 
| isFinalKeyDown | boolean | Yes| Whether the final key is pressed or released. By default, the final key is pressed.| 
| finalKeyDownDuration | Number | Yes| Duration for pressing the final key. By default, there is no requirement on the duration.|