js-apis-inputmonitor.md 6.1 KB
Newer Older
Z
zengyawen 已提交
1 2
# Input Monitor

S
shawn_he 已提交
3
The Input Monitor module implements listening for global touch events.
Z
zengyawen 已提交
4

S
shawn_he 已提交
5
> **NOTE**<br>
Z
zengyawen 已提交
6
> - The initial APIs of this module are supported since API version 7. 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
> - The APIs of this module are system APIs and cannot be called by third-party applications.


## Modules to Import


S
shawn_he 已提交
14
```js
Z
zengyawen 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
import inputMonitor from '@ohos.multimodalInput.inputMonitor';
```


## Required Permissions

ohos.permission.INPUT_MONITORING


## inputMonitor.on

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

S
shawn_he 已提交
28
Enables listening for global touch events.
Z
zengyawen 已提交
29

S
shawn_he 已提交
30 31
This is a system API.

Z
zengyawen 已提交
32 33 34 35
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
36 37
**Parameters**

S
shawn_he 已提交
38 39 40 41
| Name    | Type                                     | Mandatory| Description                           |
| -------- | ----------------------------------------- | ---- | ------------------------------- |
| type     | string                                    | Yes  | Type of the input event to listen for. The value is **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | Yes  | Callback used to return the touch event.         |
Z
zengyawen 已提交
42 43 44

  **Example**

S
shawn_he 已提交
45
```js
S
shawn_he 已提交
46 47 48 49 50 51 52 53
try {
    inputMonitor.on("touch", (data)=> {
        console.info(`monitorOnTouchEvent success ${JSON.stringify(data)}`);
        return false;
    });
} catch (error) {
    console.info("onMonitor " + error.code + " " + error.message)
}
Z
zengyawen 已提交
54
```
S
shawn_he 已提交
55

S
shawn_he 已提交
56
on(type: "mouse", receiver: Callback&lt;MouseEvent&gt;): void
S
shawn_he 已提交
57 58 59

Enables listening for global mouse events.

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

S
shawn_he 已提交
62 63 64 65
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
66
**Parameters**
S
shawn_he 已提交
67 68 69 70

| Name    | Type                | Mandatory| Description                           |
| -------- | -------------------- | ---- | ------------------------------- |
| type     | string               | Yes  | Type of the input event to listen for. The value is **mouse**.|
S
shawn_he 已提交
71
| receiver | Callback&lt;MouseEvent&gt; | Yes  | Callback used to return the mouse event.         |
S
shawn_he 已提交
72 73 74 75

  **Example**

```js
S
shawn_he 已提交
76 77 78 79 80 81 82 83
try {
    inputMonitor.on("mouse", (data)=> {
        console.info(`monitorOnMouseEvent success ${JSON.stringify(data)}`);
        return false;
    });
} catch (error) {
    console.info("onMonitor " + error.code + " " + error.message)
}
Z
zengyawen 已提交
84 85 86 87
```

## inputMonitor.off

S
shawn_he 已提交
88
off(type: "touch", receiver?: TouchEventReceiver): void
Z
zengyawen 已提交
89

S
shawn_he 已提交
90
Stops listening for global touch events.
Z
zengyawen 已提交
91

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

Z
zengyawen 已提交
94 95 96 97
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
98 99
**Parameters**

S
shawn_he 已提交
100 101 102 103
| Name    | Type                                     | Mandatory| Description                           |
| -------- | ----------------------------------------- | ---- | ------------------------------- |
| type     | string                                    | Yes  | Type of the input event to listen for. The value is **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | No  | Callback used to return the touch event.         |
Z
zengyawen 已提交
104 105 106

  **Example**

S
shawn_he 已提交
107
```js
S
shawn_he 已提交
108
// Disable listening globally.
S
shawn_he 已提交
109 110 111 112 113
try {
    inputMonitor.off("touch");
} catch (error) {
    console.info("offMonitor " + error.code + " " + error.message)
}
S
shawn_he 已提交
114
// Disable listening for this receiver.
S
shawn_he 已提交
115 116 117 118 119 120 121
callback:function(data) {
    console.info(`call success ${JSON.stringify(data)}`);
},
try {
    inputMonitor.on("touch", this.callback);
} catch (error) {
    console.info("onTouchMonitor " + error.code + " " + error.message)
S
shawn_he 已提交
122
},
S
shawn_he 已提交
123 124 125 126 127
try {
    inputMonitor.off("touch",this.callback);
} catch (error) {
    console.info("offTouchMonitor " + error.code + " " + error.message)
}
Z
zengyawen 已提交
128
```
S
shawn_he 已提交
129

130
off(type: "mouse", receiver?:Callback\<MouseEvent>):void
S
shawn_he 已提交
131 132 133

Stops listening for global mouse events.

S
shawn_he 已提交
134 135
This is a system API.

S
shawn_he 已提交
136 137 138 139
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
140
**Parameters**
S
shawn_he 已提交
141 142 143 144

| Name    | Type                | Mandatory| Description                           |
| -------- | -------------------- | ---- | ------------------------------- |
| type     | string               | Yes  | Type of the input event to listen for. The value is **mouse**.|
S
shawn_he 已提交
145
| receiver | Callback&lt;MouseEvent&gt; | No  | Callback used to return the mouse event.         |
S
shawn_he 已提交
146 147 148 149

**Example**

```js
S
shawn_he 已提交
150
// Disable listening globally.
S
shawn_he 已提交
151 152 153 154 155
try {
    inputMonitor.off("mouse");
} catch (error) {
    console.info("offMonitor " + error.code + " " + error.message)
}
S
shawn_he 已提交
156
// Disable listening for this receiver.
S
shawn_he 已提交
157 158 159 160 161 162 163
callback:function(data) {
    console.info(`call success ${JSON.stringify(data)}`);
},
try {
    inputMonitor.on("mouse", this.callback);
} catch (error) {
    console.info("onMouseMonitor " + error.code + " " + error.message)
S
shawn_he 已提交
164
},
S
shawn_he 已提交
165 166 167 168 169
try {
    inputMonitor.off("mouse", this.callback);
} catch (error) {
    console.info("offMouseMonitor " + error.code + " " + error.message)
}
Z
zengyawen 已提交
170 171 172
```


S
shawn_he 已提交
173

Z
zengyawen 已提交
174 175
## TouchEventReceiver

S
shawn_he 已提交
176
Provides the callback of touch events.
Z
zengyawen 已提交
177

S
shawn_he 已提交
178
This is a system API.
Z
zengyawen 已提交
179 180 181

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

S
shawn_he 已提交
182 183
**Parameters**

E
ester.zhou 已提交
184 185 186
| Name        | Type                                      | Mandatory  | Description                                      |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
| touchEvent | [TouchEvent](../arkui-js/js-components-common-events.md) | Yes   | Callback used to return the touch event.|
Z
zengyawen 已提交
187

S
shawn_he 已提交
188 189
**Return value**

E
ester.zhou 已提交
190 191 192
| Type     | Description                                    |
| ------- | -------------------------------------- |
| Boolean | Result indicating whether the touch event has been consumed by the input monitor. The value **true** indicates that the touch event has been consumed, and the value **false** indicates the opposite.|
Z
zengyawen 已提交
193 194 195

  **Example**

S
shawn_he 已提交
196
```js
S
shawn_he 已提交
197 198
try {
  inputMonitor.on("touch", (event) => {
S
shawn_he 已提交
199
    // If true is returned, all subsequent events of this operation will be consumed by the listener, instead of being distributed to the window.
S
shawn_he 已提交
200 201 202 203 204 205
    return false;
  });
  inputMonitor.off("touch");
} catch (error) {
  console.info("offMonitor " + error.code + " " + error.message)
}
Z
zengyawen 已提交
206
```