js-apis-inputmonitor.md 4.5 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 46 47 48 49
```js
inputMonitor.off("touch", (event) => {
  // A touch event is consumed.
  return false;
});
Z
zengyawen 已提交
50
```
S
shawn_he 已提交
51

S
shawn_he 已提交
52
on(type: "mouse", receiver: Callback&lt;MouseEvent&gt;): void
S
shawn_he 已提交
53 54 55

Enables listening for global mouse events.

S
shawn_he 已提交
56 57
This is a system API.

S
shawn_he 已提交
58 59 60 61
**Required permissions**: ohos.permission.INPUT_MONITORING

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

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

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

  **Example**

```js
inputMonitor.off("mouse", (event) => {
  // A mouse event is consumed.
});
Z
zengyawen 已提交
75 76 77
```


S
shawn_he 已提交
78

Z
zengyawen 已提交
79 80
## inputMonitor.off

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

S
shawn_he 已提交
83
Stops listening for global touch events.
Z
zengyawen 已提交
84

S
shawn_he 已提交
85 86
This is a system API.

Z
zengyawen 已提交
87 88 89 90
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
91 92
**Parameters**

S
shawn_he 已提交
93 94 95 96
| 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 已提交
97 98 99

  **Example**

S
shawn_he 已提交
100 101
```js
inputMonitor.off("touch");
Z
zengyawen 已提交
102
```
S
shawn_he 已提交
103

104
off(type: "mouse", receiver?:Callback\<MouseEvent>):void
S
shawn_he 已提交
105 106 107

Stops listening for global mouse events.

S
shawn_he 已提交
108 109
This is a system API.

S
shawn_he 已提交
110 111 112 113
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
114
**Parameters**
S
shawn_he 已提交
115 116 117 118

| Name    | Type                | Mandatory| Description                           |
| -------- | -------------------- | ---- | ------------------------------- |
| type     | string               | Yes  | Type of the input event to listen for. The value is **mouse**.|
S
shawn_he 已提交
119
| receiver | Callback&lt;MouseEvent&gt; | No  | Callback used to return the mouse event.         |
S
shawn_he 已提交
120 121 122 123 124

**Example**

```js
inputMonitor.off("mouse");
Z
zengyawen 已提交
125 126 127
```


S
shawn_he 已提交
128

Z
zengyawen 已提交
129 130
## TouchEventReceiver

S
shawn_he 已提交
131
Provides the callback of touch events.
Z
zengyawen 已提交
132

S
shawn_he 已提交
133
This is a system API.
Z
zengyawen 已提交
134 135 136

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

S
shawn_he 已提交
137 138
**Parameters**

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

S
shawn_he 已提交
143 144
**Return value**

E
ester.zhou 已提交
145 146 147
| 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 已提交
148 149 150

  **Example**

S
shawn_he 已提交
151 152 153 154 155 156
```js
inputMonitor.on("touch", (event) => {
  // A touch event is consumed.
  return false;
});
inputMonitor.off("touch");
Z
zengyawen 已提交
157
```