js-apis-inputmonitor.md 15.6 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, including the touchscreen, mouse, touchpad, etc.
Z
zengyawen 已提交
4

S
shawn_he 已提交
5
>**NOTE**
S
shawn_he 已提交
6
>
S
shawn_he 已提交
7 8 9 10 11
>- 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.
>
>- In this document, **global** indicates the entire touchscreen or touchpad. For example, listening for global touch events means to listen for touch events of the entire touchpad when a user touches at any position on the touchpad.
Z
zengyawen 已提交
12 13 14

## Modules to Import

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

S
shawn_he 已提交
19
## inputMonitor.on('touch')
Z
zengyawen 已提交
20

S
shawn_he 已提交
21
on(type: 'touch', receiver: TouchEventReceiver): void
Z
zengyawen 已提交
22

S
shawn_he 已提交
23
Enables listening for global touch (touchscreen) 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
| Name      | Type                                      | Mandatory  | Description                 |
| -------- | ---------------------------------------- | ---- | ------------------- |
S
shawn_he 已提交
33 34
| type     | string                                   | Yes   | Event type. This field has a fixed value of **touch**.|
| receiver | [TouchEventReceiver](#toucheventreceiver) | Yes   | Callback used to return the touch event asynchronously.|
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
  inputMonitor.on('touch', (touchEvent) => {
S
shawn_he 已提交
41 42 43
    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
## inputMonitor.on('mouse')<sup>9+</sup>
S
shawn_he 已提交
50

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
| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
S
shawn_he 已提交
63 64
| type     | string                     | Yes   | Event type. This field has a fixed value of **mouse**.|
| receiver | Callback&lt;MouseEvent&gt; | Yes   | Callback used to return the mouse event asynchronously. |
S
shawn_he 已提交
65 66 67 68

  **Example**

```js
S
shawn_he 已提交
69
try {
S
shawn_he 已提交
70
  inputMonitor.on('mouse', (mouseEvent) => {
S
shawn_he 已提交
71 72 73
    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
## inputMonitor.off('touch')
S
shawn_he 已提交
80

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

S
shawn_he 已提交
83
Disables listening for global touch (touchscreen) events.
S
shawn_he 已提交
84

Z
zengyawen 已提交
85 86 87 88
**Required permissions**: ohos.permission.INPUT_MONITORING

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

S
shawn_he 已提交
89 90
**Parameters**

S
shawn_he 已提交
91 92
| Name      | Type                                      | Mandatory  | Description                 |
| -------- | ---------------------------------------- | ---- | ------------------- |
S
shawn_he 已提交
93
| type     | string                                   | Yes   | Event type. This field has a fixed value of **touch**.|
S
shawn_he 已提交
94
| 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 已提交
95

S
shawn_he 已提交
96
**Example**
Z
zengyawen 已提交
97

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

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

S
shawn_he 已提交
128
## inputMonitor.off('mouse')<sup>9+</sup>
S
shawn_he 已提交
129

S
shawn_he 已提交
130
off(type: 'mouse', receiver?: Callback&lt;MouseEvent&gt;): void
S
shawn_he 已提交
131

S
shawn_he 已提交
132
Disables listening for global mouse events.
S
shawn_he 已提交
133

S
shawn_he 已提交
134 135 136 137
**Required permissions**: ohos.permission.INPUT_MONITORING

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

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

S
shawn_he 已提交
140 141
| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
S
shawn_he 已提交
142
| type     | string                     | Yes   | Event type. This field has a fixed value of **mouse**.|
S
shawn_he 已提交
143
| 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 已提交
144 145 146 147

**Example**

```js
S
shawn_he 已提交
148
// Disable listening for a single callback.
149
let callback = (mouseEvent: MouseEvent) => {
S
shawn_he 已提交
150 151 152
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};
S
shawn_he 已提交
153
try {
S
shawn_he 已提交
154 155
  inputMonitor.on('mouse', callback);
  inputMonitor.off('mouse', callback);
S
shawn_he 已提交
156
  console.log(`Monitor off success`);
S
shawn_he 已提交
157
} catch (error) {
S
shawn_he 已提交
158
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
159
}
S
shawn_he 已提交
160 161 162
```

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

## TouchEventReceiver

S
shawn_he 已提交
179
Defines the callback for touch (touchscreen) events.
Z
zengyawen 已提交
180

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

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

S
shawn_he 已提交
185 186
**Parameters**

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

S
shawn_he 已提交
191 192
**Return value**

S
shawn_he 已提交
193 194 195
| 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 已提交
196

S
shawn_he 已提交
197
**Example**
Z
zengyawen 已提交
198

S
shawn_he 已提交
199
```js
S
shawn_he 已提交
200
try {
S
shawn_he 已提交
201
  inputMonitor.on('touch', touchEvent => {
S
shawn_he 已提交
202 203 204
    if (touchEvent.touches.length == 3) {// Three fingers are pressed.
      return true;
    }
S
shawn_he 已提交
205
    return false;
S
shawn_he 已提交
206 207
  });
} catch (error) {
S
shawn_he 已提交
208
    console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
S
shawn_he 已提交
209
}
Z
zengyawen 已提交
210
```
S
shawn_he 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

## inputMonitor.on('pinch')<sup>10+</sup>

on(type: 'pinch', receiver: Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt;): void

Enables listening for global touchpad pinch events.

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

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

**Parameters**

| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Event type. This field has a fixed value of **pinch**.|
| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt; | Yes   | Callback used to return the pinch event asynchronously. |

  **Example**

```js
try {
  inputMonitor.on('pinch', (pinchEvent) => {
    console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
    return false;
  });
} catch (error) {
  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## inputMonitor.off('pinch')<sup>10+</sup>

off(type: 'pinch', receiver?: Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt;): void

Disables listening for global touchpad pinch events.

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

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

**Parameters**

| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Event type. This field has a fixed value of **pinch**.|
| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&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.|

**Example**

```js
// Disable listening for a single callback.
function callback(pinchEvent) {
  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
  return false;
};
try {
  inputMonitor.on('pinch', callback);
  inputMonitor.off('pinch', callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

```js
// Cancel listening for all callbacks.
function callback(pinchEvent) {
  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
  return false;
};
try {
  inputMonitor.on('pinch', callback);
  inputMonitor.off('pinch');
  console.log(`Monitor off success`);
} catch (error) {
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## inputMonitor.on('threeFingersSwipe')<sup>10+</sup>

on(type: 'threeFingersSwipe', receiver: Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt;): void

Enables listening for three-finger swipe events.

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

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

**Parameters**

| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Event type. This field has a fixed value of **threeFingersSwipe**.|
| receiver | Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt; | Yes   | Callback used to return the three-finger swipe event asynchronously. |

  **Example**

```js
try {
  inputMonitor.on('threeFingersSwipe', (threeFingersSwipe) => {
    console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
    return false;
  });
} catch (error) {
  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## inputMonitor.off('threeFingersSwipe')<sup>10+</sup>

off(type: 'threeFingersSwipe', receiver?: Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt;): void

Disables listening for three-finger swipe events.

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

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

**Parameters**

| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Event type. This field has a fixed value of **threeFingersSwipe**.|
| receiver | Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&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.|

**Example**

```js
// Disable listening for a single callback.
function callback(threeFingersSwipe) {
  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
  return false;
};
try {
  inputMonitor.on('threeFingersSwipe', callback);
  inputMonitor.off("threeFingersSwipe", callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

```js
// Cancel listening for all callbacks.
function callback(threeFingersSwipe) {
  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
  return false;
};
try {
  inputMonitor.on("threeFingersSwipe", callback);
  inputMonitor.off("threeFingersSwipe");
  console.log(`Monitor off success`);
} catch (error) {
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## inputMonitor.on('fourFingersSwipe')<sup>10+</sup>

on(type: 'fourFingersSwipe', receiver: Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt;): void

Enables listening for four-finger swipe events.

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

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

**Parameters**

| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Event type. This field has a fixed value of **fourFingersSwipe**.|
| receiver | Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt; | Yes   | Callback used to return the four-finger swipe event asynchronously. |

  **Example**

```js
try {
  inputMonitor.on('fourFingersSwipe', (fourFingersSwipe) => {
    console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
    return false;
  });
} catch (error) {
  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## inputMonitor.off('fourFingersSwipe')<sup>10+</sup>

off(type: 'fourFingersSwipe', receiver?: Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt;): void

Disables listening for four-finger swipe events.

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

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

**Parameters**

| Name      | Type                        | Mandatory  | Description                 |
| -------- | -------------------------- | ---- | ------------------- |
| type     | string                     | Yes   | Event type. This field has a fixed value of **fourFingersSwipe**.|
| receiver | Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&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.|

**Example**

```js
// Disable listening for a single callback.
function callback(fourFingersSwipe) {
  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
  return false;
};
try {
  inputMonitor.on('fourFingersSwipe', callback);
  inputMonitor.off('fourFingersSwipe', callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

```js
// Cancel listening for all callbacks.
function callback(fourFingersSwipe) {
  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
  return false;
};
try {
  inputMonitor.on('fourFingersSwipe', callback);
  inputMonitor.off('fourFingersSwipe');
  console.log(`Monitor off success`);
} catch (error) {
  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```