js-apis-inputconsumer.md 3.5 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
# Combination Key


> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> - 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.
> 
> - 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

on(type: "key", keyOption: KeyOption, callback: Callback<KeyOption>): void

Enables listening for combination key events. When a combination key event that meets the specified conditions occurs, **keyOption** will be passed as an input parameter to **callback**.

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

  **Parameters**
  | Name | Type | Mandatory | Description | 
| -------- | -------- | -------- | -------- |
| type | string | Yes | Type of the key input event to listen for. Only **key** is supported. | 
| keyOption | [KeyOption](#keyoption) | Yes | Key option, which specifies the condition for combination key input. | 
| callback | Callback<KeyOption> | Yes | Callback function. When a key input event that meets the specified options occurs, **keyOption** will be passed as an input parameter to **callback**. | 

  **Example**

```
let keyOption = {preKeys: [], finalKey: 3, isFinalKeyDown: true, finalKeyDownDuration: 0}
let callback = function(keyOption) {
    console.info("preKeys: " + keyOption.preKeys, "finalKey: " + keyOption.finalKey, 
                 "isFinalKeyDown: " + keyOption.isFinalKeyDown, "finalKeyDownDuration: " + keyOption.finalKeyDownDuration)
}
inputConsumer.on('key', keyOption, callback);
```


## inputConsumer.off

off(type: "key", keyOption: KeyOption, callback: Callback<KeyOption>): void

Stops listening for combination key events.

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

  **Parameters**
  | Name | Type | Mandatory | Description | 
| -------- | -------- | -------- | -------- |
| type | string | Yes | Type of the key input event to listen for. Only **key** is supported. | 
| keyOption | [KeyOption](#keyoption) | Yes | Key option passed to the key input event when listening starts. | 
| callback | Callback<KeyOption> | Yes | Callback function passed to the key input event with the key option when listening starts. | 

  **Example**

```
let keyOption = {preKeys: [], finalKey: 3, isFinalKeyDown: true, finalKeyDownDuration: 0}
let callback = function(keyOption) {
    console.info("preKeys: " + keyOption.preKeys, "finalKey: " + keyOption.finalKey, 
                 "isFinalKeyDown: " + keyOption.isFinalKeyDown, "finalKeyDownDuration: " + keyOption.finalKeyDownDuration)
}
inputConsumer.off('key', keyOption, callback);
```


## KeyOption

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

  **System capability**: SystemCapability.MultimodalInput.Input.InputConsumer
  | Name | Type | Mandatory | Description | 
| -------- | -------- | -------- | -------- |
| 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 | Indicates 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. |