js-apis-inputconsumer.md 3.5 KB
Newer Older
Z
zengyawen 已提交
1 2 3
# Combination Key


S
shawn_he 已提交
4 5
> **NOTE**<br>
>
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
>
Z
zengyawen 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
> - 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 已提交
21
on(type: "key", keyOptions: KeyOptions, callback: Callback<KeyOptions>): void
Z
zengyawen 已提交
22

S
shawn_he 已提交
23
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 已提交
24 25 26

**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

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

| Name| Type| Mandatory| Description| 
Z
zengyawen 已提交
30
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
31 32 33
| 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 | KeyOptions | 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 已提交
34

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

```
S
shawn_he 已提交
38 39 40 41
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 已提交
42
}
S
shawn_he 已提交
43
inputConsumer.on('key', keyOptions, callback);
Z
zengyawen 已提交
44 45 46 47 48
```


## inputConsumer.off

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

Stops listening for combination key events.

**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

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

| Name| Type| Mandatory| Description| 
Z
zengyawen 已提交
58
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
59 60 61
| 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.| 
| callback | Callback<KeyOptions> | Yes| Callback function passed to the key input event with **keyOptions** when listening starts.| 
Z
zengyawen 已提交
62

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

```
S
shawn_he 已提交
66 67 68 69
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 已提交
70
}
S
shawn_he 已提交
71
inputConsumer.off('key', keyOptions, callback);
Z
zengyawen 已提交
72 73 74
```


S
shawn_he 已提交
75
## keyOptions
Z
zengyawen 已提交
76 77 78

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

S
shawn_he 已提交
79 80 81 82 83
**System capability**: SystemCapability.MultimodalInput.Input.InputConsumer

**Parameters**

| Name| Type| Mandatory| Description| 
Z
zengyawen 已提交
84
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
85 86 87 88
| 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.|