js-apis-inputmonitor.md 6.7 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.multimodalInput.inputMonitor (Input Monitor)
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
The **inputMonitor** module implements listening for events of input devices (namely, touchscreen and mouse). 
Z
zengyawen 已提交
4

S
shawn_he 已提交
5
>  **NOTE**
S
shawn_he 已提交
6
>
S
shawn_he 已提交
7 8
>  - 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.
>  - The APIs provided by this module are system APIs.
Z
zengyawen 已提交
9 10 11 12 13


## Modules to Import


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


## inputMonitor.on

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

S
shawn_he 已提交
23
Enables listening for global touch events.
Z
zengyawen 已提交
24 25 26 27 28

**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
29 30
**Parameters**

S
shawn_he 已提交
31 32 33 34
| Name      | Type                                      | Mandatory  | Description                 |
| -------- | ---------------------------------------- | ---- | ------------------- |
| type     | string                                   | Yes   | Type of the input device event. The value is **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | Yes   | Callback used to return the touch event.|
Z
zengyawen 已提交
35

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

S
shawn_he 已提交
38
```js
S
shawn_he 已提交
39
try {
S
shawn_he 已提交
40 41 42 43
  inputMonitor.on("touch", (touchEvent) => {
    console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
    return false;
  });
S
shawn_he 已提交
44
} catch (error) {
S
shawn_he 已提交
45
  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
46
}
Z
zengyawen 已提交
47
```
S
shawn_he 已提交
48

S
shawn_he 已提交
49 50
## inputMonitor.on<sup>9+</sup>

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

Enables listening for global mouse events.

**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
59
**Parameters**
S
shawn_he 已提交
60

S
shawn_he 已提交
61 62 63 64
| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Type of the input device event. The value is **mouse**.|
| receiver | Callback&lt;MouseEvent&gt; | Yes   | Callback used to return the mouse event. |
S
shawn_he 已提交
65 66 67 68

  **Example**

```js
S
shawn_he 已提交
69
try {
S
shawn_he 已提交
70 71 72 73
  inputMonitor.on("mouse", (mouseEvent) => {
    console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
    return false;
  });
S
shawn_he 已提交
74
} catch (error) {
S
shawn_he 已提交
75
  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
76
}
Z
zengyawen 已提交
77 78
```

S
shawn_he 已提交
79 80


Z
zengyawen 已提交
81 82
## inputMonitor.off

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

S
shawn_he 已提交
85
Disables listening for global touch events.
S
shawn_he 已提交
86

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 device event. The value is **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | No   | Callback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application. |
Z
zengyawen 已提交
97

S
shawn_he 已提交
98
**Example**
Z
zengyawen 已提交
99

S
shawn_he 已提交
100
```js
S
shawn_he 已提交
101 102 103 104 105
// Disable listening for a single callback function.
function callback(touchEvent) {
  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
  return false;
};
S
shawn_he 已提交
106
try {
S
shawn_he 已提交
107 108 109
  inputMonitor.on("touch", callback);
  inputMonitor.off("touch", callback);
  console.log(`Monitor off success`);
S
shawn_he 已提交
110
} catch (error) {
S
shawn_he 已提交
111
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
112
}
S
shawn_he 已提交
113 114 115 116 117 118 119 120
```

```js
// Cancel listening for all callback functions.
function callback(touchEvent) {
  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
  return false;
};
S
shawn_he 已提交
121
try {
S
shawn_he 已提交
122 123 124
  inputMonitor.on("touch", callback);
  inputMonitor.off("touch");
  console.log(`Monitor off success`);
S
shawn_he 已提交
125
} catch (error) {
S
shawn_he 已提交
126
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
127
}
Z
zengyawen 已提交
128
```
S
shawn_he 已提交
129

S
shawn_he 已提交
130
## inputMonitor.off<sup>9+</sup>
S
shawn_he 已提交
131

S
shawn_he 已提交
132
off(type: "mouse", receiver?: Callback&lt;MouseEvent&gt;): void
S
shawn_he 已提交
133

S
shawn_he 已提交
134
Stops listening for global mouse events.
S
shawn_he 已提交
135

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

S
shawn_he 已提交
142 143 144 145
| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Type of the input device event. The value is **mouse**.|
| receiver | Callback&lt;MouseEvent&gt; | No   | Callback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.|
S
shawn_he 已提交
146 147 148 149

**Example**

```js
S
shawn_he 已提交
150 151 152 153 154
// Disable listening for a single callback.
function callback(mouseEvent) {
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};
S
shawn_he 已提交
155
try {
S
shawn_he 已提交
156 157 158
  inputMonitor.on("mouse", callback);
  inputMonitor.off("mouse", callback);
  console.log(`Monitor off success`);
S
shawn_he 已提交
159
} catch (error) {
S
shawn_he 已提交
160
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
161
}
S
shawn_he 已提交
162 163 164 165 166 167 168 169
```

```js
// Disable listening for all callback functions.
function callback(mouseEvent) {
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};
S
shawn_he 已提交
170
try {
S
shawn_he 已提交
171 172 173
  inputMonitor.on("mouse", callback);
  inputMonitor.off("mouse");
  console.log(`Monitor off success`);
S
shawn_he 已提交
174
} catch (error) {
S
shawn_he 已提交
175
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
176
}
Z
zengyawen 已提交
177 178 179 180
```

## TouchEventReceiver

S
shawn_he 已提交
181
Represents the callback used to return the touch event.
Z
zengyawen 已提交
182

S
shawn_he 已提交
183
**Required permissions**: ohos.permission.INPUT_MONITORING
Z
zengyawen 已提交
184 185 186

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

S
shawn_he 已提交
187 188
**Parameters**

E
ester.zhou 已提交
189 190
| Name        | Type                                      | Mandatory  | Description                                      |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
S
shawn_he 已提交
191
| touchEvent | [TouchEvent](../arkui-js/js-components-common-events.md) | Yes   | Touch event.|
Z
zengyawen 已提交
192

S
shawn_he 已提交
193 194
**Return value**

S
shawn_he 已提交
195 196 197
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| Boolean | Result indicating whether the touch event will be dispatched to the window. The value **true** indicates that the touch event will be dispatched to the window, and the value **false** indicates the opposite.|
Z
zengyawen 已提交
198

S
shawn_he 已提交
199
**Example**
Z
zengyawen 已提交
200

S
shawn_he 已提交
201
```js
S
shawn_he 已提交
202
try {
S
shawn_he 已提交
203 204 205 206 207 208
  inputMonitor.on("touch", touchEvent => {
    if (touchEvent.touches.length == 3) {// Three fingers are pressed.
      return true;
    } else {
      return false;
    }
S
shawn_he 已提交
209 210
  });
} catch (error) {
S
shawn_he 已提交
211
    console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
212
}
Z
zengyawen 已提交
213
```