js-apis-inputconsumer.md 3.9 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
> **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
>
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<KeyOptions>): 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
| type | string | Yes| Type of the key input event to listen for. Only **key** is supported.|
S
shawn_he 已提交
35
| keyOptions | [keyOptions](#keyoptions) | Yes| Key option, which specifies the condition for combination key input.|
S
shawn_he 已提交
36
| 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 45 46 47 48 49
let keyOptions = { preKeys: [], finalKey: 18, isFinalKeyDown: true, finalKeyDownDuration: 0 }
let callback = function (keyOptions) {
  console.info("preKeys: " + keyOptions.preKeys, "finalKey: " + keyOptions.finalKey,
    "isFinalKeyDown: " + keyOptions.isFinalKeyDown, "finalKeyDownDuration: " + keyOptions.finalKeyDownDuration)
}
try {
  inputConsumer.on(inputConsumer.SubscribeType.KEY, keyOptions, callback);
} catch (error) {
  console.info(`inputConsumer.on, error.code=${JSON.stringify(error.code)}, error.msg=${JSON.stringify(error.message)}`);
Z
zengyawen 已提交
50 51 52 53 54 55
}
```


## inputConsumer.off

S
shawn_he 已提交
56
off(type: "key", keyOptions: KeyOptions, callback?: Callback&lt;KeyOptions&gt;): void
Z
zengyawen 已提交
57 58 59

Stops listening for combination key events.

S
shawn_he 已提交
60 61
This is a system API.

Z
zengyawen 已提交
62 63
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

S
shawn_he 已提交
64 65 66
**Parameters**

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

S
shawn_he 已提交
72
**Example**
Z
zengyawen 已提交
73 74

```
S
shawn_he 已提交
75 76 77 78 79 80 81 82 83
let keyOptions = { preKeys: [], finalKey: 18, isFinalKeyDown: true, finalKeyDownDuration: 0 }
let callback = function (keyOptions) {
  console.info("preKeys: " + keyOptions.preKeys, "finalKey: " + keyOptions.finalKey,
    "isFinalKeyDown: " + keyOptions.isFinalKeyDown, "finalKeyDownDuration: " + keyOptions.finalKeyDownDuration)
}
try {
  inputConsumer.off(inputConsumer.SubscribeType.KEY, keyOptions, callback);
} catch (error) {
  console.info(`inputConsumer.off, error.code=${JSON.stringify(error.code)}, error.msg=${JSON.stringify(error.message)}`);
Z
zengyawen 已提交
84 85 86 87
}
```


S
shawn_he 已提交
88
## keyOptions
Z
zengyawen 已提交
89 90 91

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

S
shawn_he 已提交
92 93
This is a system API.

S
shawn_he 已提交
94 95 96 97 98
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

**Parameters**

| Name| Type| Mandatory| Description| 
Z
zengyawen 已提交
99
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
100 101 102 103
| 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.|