js-apis-inputmonitor.md 4.5 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5
# 输入监听


> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
H
HelloCrease 已提交
6
>
Z
zengyawen 已提交
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
> - 本模块接口均为系统接口,三方应用不支持调用。


## 导入模块


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


## 权限

ohos.permission.INPUT_MONITORING


## inputMonitor.on

on(type: "touch", receiver: TouchEventReceiver): void

开始监听全局输入。

**需要权限:**ohos.permission.INPUT_MONITORING

**系统能力:**SystemCapability.MultimodalInput.Input.InputMonitor

  **参数:**
H
HelloCrease 已提交
34 35 36 37
| 参数       | 类型                                       | 必填   | 说明                   |
| -------- | ---------------------------------------- | ---- | -------------------- |
| type     | string                                   | 是    | 监听输入事件类型,只支持“touch”。 |
| receiver | [TouchEventReceiver](#toucheventreceiver) | 是    | 触摸输入事件回调函数。          |
Z
zengyawen 已提交
38 39 40 41

  **示例:**

```
H
HelloCrease 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
export default {
    callback: function (value) {
        if (checkEvent(value)) {
            //事件满足业务要求,事件被消费
            return true;
        } else {
            //事件不满足业务要求,事件未被消费
            return false;
        }
    },
    testOn: function () {
        console.info("InputMonitorJsTest---start---testOn");
        inputMonitor.on(
            "touch",
            this.callback
        );
        console.info("InputMonitorJsTest---end---testOn");
Z
zengyawen 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    }
}
```


## inputMonitor.off

off(type: "touch", receiver: TouchEventReceiver): void

停止监听全局输入。

**需要权限:**ohos.permission.INPUT_MONITORING

**系统能力:**SystemCapability.MultimodalInput.Input.InputMonitor

  **参数:**
H
HelloCrease 已提交
75 76 77 78
| 参数       | 类型                                       | 必填   | 说明                   |
| -------- | ---------------------------------------- | ---- | -------------------- |
| type     | string                                   | 是    | 监听输入事件类型,只支持“touch”。 |
| receiver | [TouchEventReceiver](#toucheventreceiver) | 否    | 触摸输入事件回调函数。          |
Z
zengyawen 已提交
79 80 81 82

  **示例:**

```
H
HelloCrease 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
export default {
    callback: function (value) {
        if (checkEvent(value)) {
            //事件满足业务要求,事件被消费
            return true;
        } else {
            //事件不满足业务要求,事件未被消费
            return false;
        }
    },
    testOff: function () {
        console.info("InputMonitorJsTest---start---testOff");
        inputMonitor.off(
            "touch",
            this.callback
        );
        console.info("InputMonitorJsTest---end---testOff");
Z
zengyawen 已提交
100
    }
H
HelloCrease 已提交
101
  }
Z
zengyawen 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
```


## TouchEventReceiver

触摸输入事件的回调函数,如果返回true,则触摸输入将被监听器消耗(系统执行关闭动作)。


### (touchEvent: TouchEvent): Boolean

触摸输入事件的回调函数。函数名由使用者定义,这里是函数调用时必须符合的格式,传入参数必须为TouchEvent类型,返回值为Boolean类型。

**系统能力:**SystemCapability.MultimodalInput.Input.InputMonitor

  **参数:**
H
HelloCrease 已提交
117 118 119
| 参数         | 类型                                       | 必填   | 说明                                       |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
| touchEvent | [TouchEvent](../arkui-js/js-components-common-events.md) | 是    | 触摸输入事件回调函数,返回true表示输触事件被监听器消费,false表示输触事件未被监听器消费。 |
Z
zengyawen 已提交
120 121

  **返回值:**
H
HelloCrease 已提交
122 123 124
| 类型      | 说明                                     |
| ------- | -------------------------------------- |
| Boolean | 返回true表示输触事件被监听器消费,false表示输触事件未被监听器消费。 |
Z
zengyawen 已提交
125 126 127 128

  **示例:**

```
H
HelloCrease 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
export default {
    callback: function (value) {  //此处为(touchEvent:TouchEvent): Boolean 方法的实现
        if (checkEvent(value)) {
            //事件满足业务要求,事件被消费
            return true;
        } else {
            //事件不满足业务要求,事件未被消费
            return false;
        }
    },
    testOff: function () {
        console.info("InputMonitorJsTest---start---testOff");
        inputMonitor.off(
            "touch",
            this.callback
        );
        console.info("InputMonitorJsTest---end---testOff");
Z
zengyawen 已提交
146 147 148
    }
}
```