• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputMonitor (Input Monitor)
2
3The **inputMonitor** module provides APIs to listen for touch events of input devices including touchscreen, mouse, and touchpad.
4
5>  **NOTE**
6>
7>  - 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.
8>
9>  - The APIs provided by this module are system APIs.
10
11
12## Modules to Import
13
14
15```js
16import inputMonitor from '@ohos.multimodalInput.inputMonitor';
17```
18
19
20## inputMonitor.on('touch')
21
22on(type: 'touch', receiver: TouchEventReceiver): void
23
24Enables listening for global touch events.
25
26**Required permissions**: ohos.permission.INPUT_MONITORING
27
28**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
29
30**Parameters**
31
32| Name      | Type                                      | Mandatory  | Description                 |
33| -------- | ---------------------------------------- | ---- | ------------------- |
34| type     | string                                   | Yes   | Type of the input device event. The value is **touch**.|
35| receiver | [TouchEventReceiver](#toucheventreceiver) | Yes   | Callback used to return the touch event asynchronously.|
36
37**Example**
38
39```js
40try {
41  inputMonitor.on('touch', (touchEvent: TouchEvent) => {
42    console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
43    return false;
44  });
45} catch (error) {
46  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
47}
48```
49
50## inputMonitor.on('mouse')<sup>9+</sup>
51
52on(type: 'mouse', receiver: Callback&lt;MouseEvent&gt;): void
53
54Enables listening for global mouse events.
55
56**Required permissions**: ohos.permission.INPUT_MONITORING
57
58**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
59
60**Parameters**
61
62| Name      | Type                        | Mandatory  | Description                 |
63| -------- | -------------------------- | ---- | ------------------- |
64| type     | string                     | Yes   | Type of the input device event. The value is **mouse**.|
65| receiver | Callback&lt;MouseEvent&gt; | Yes   | Callback used to return the mouse event asynchronously. |
66
67  **Example**
68
69```js
70import { MouseEvent } from '@ohos.multimodalInput.mouseEvent';
71
72try {
73  inputMonitor.on('mouse', (mouseEvent: MouseEvent) => {
74    console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
75    return false;
76  });
77} catch (error) {
78  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
79}
80```
81
82
83
84## inputMonitor.off('touch')
85
86off(type: 'touch', receiver?: TouchEventReceiver): void
87
88Disables listening for global touch events.
89
90**Required permissions**: ohos.permission.INPUT_MONITORING
91
92**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
93
94**Parameters**
95
96| Name      | Type                                      | Mandatory  | Description                 |
97| -------- | ---------------------------------------- | ---- | ------------------- |
98| type     | string                                   | Yes   | Type of the input device event. The value is **touch**.|
99| 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. |
100
101**Example**
102
103```js
104// Disable listening for a single callback.
105let callback = (touchEvent: TouchEvent) => {
106  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
107  return false;
108};
109try {
110  inputMonitor.on('touch', callback);
111  inputMonitor.off('touch', callback);
112  console.log(`Monitor off success`);
113} catch (error) {
114  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
115}
116```
117
118```js
119// Cancel listening for all callbacks.
120let callback = (touchEvent: TouchEvent) => {
121  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
122  return false;
123};
124try {
125  inputMonitor.on('touch', callback);
126  inputMonitor.off('touch');
127  console.log(`Monitor off success`);
128} catch (error) {
129  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
130}
131```
132
133## inputMonitor.off('mouse')<sup>9+</sup>
134
135off(type: 'mouse', receiver?: Callback&lt;MouseEvent&gt;): void
136
137Disables listening for global mouse events.
138
139**Required permissions**: ohos.permission.INPUT_MONITORING
140
141**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
142
143**Parameters**
144
145| Name      | Type                        | Mandatory  | Description                 |
146| -------- | -------------------------- | ---- | ------------------- |
147| type     | string                     | Yes   | Type of the input device event. The value is **mouse**.|
148| 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.|
149
150**Example**
151
152```js
153// Disable listening for a single callback.
154let callback = (mouseEvent: MouseEvent) => {
155  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
156  return false;
157};
158try {
159  inputMonitor.on('mouse', callback);
160  inputMonitor.off('mouse', callback);
161  console.log(`Monitor off success`);
162} catch (error) {
163  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
164}
165```
166
167```js
168// Cancel listening for all callbacks.
169let callback = (mouseEvent: MouseEvent) => {
170  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
171  return false;
172};
173try {
174  inputMonitor.on('mouse', callback);
175  inputMonitor.off('mouse');
176  console.log(`Monitor off success`);
177} catch (error) {
178  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
179}
180```
181
182## TouchEventReceiver
183
184Represents the callback used to return the touch event.
185
186**Required permissions**: ohos.permission.INPUT_MONITORING
187
188**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
189
190**Parameters**
191
192| Name        | Type                                      | Mandatory  | Description                                      |
193| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
194| touchEvent | [TouchEvent](../arkui-js/js-components-common-events.md) | Yes   | Touch event.|
195
196**Return value**
197
198| Type     | Description                                      |
199| ------- | ---------------------------------------- |
200| 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.|
201
202**Example**
203
204```js
205try {
206  inputMonitor.on('touch', touchEvent => {
207    if (touchEvent.touches.length == 3) {// Three fingers are pressed.
208      return true;
209    }
210    return false;
211  });
212} catch (error) {
213    console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
214}
215```
216
217## inputMonitor.on('pinch')<sup>10+</sup>
218
219on(type: 'pinch', receiver: Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt;): void
220
221Enables listening for global touchpad pinch events.
222
223**Required permissions**: ohos.permission.INPUT_MONITORING
224
225**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
226
227**Parameters**
228
229| Name      | Type                        | Mandatory  | Description                 |
230| -------- | -------------------------- | ---- | ------------------- |
231| type     | string                     | Yes   | Event type. This field has a fixed value of **pinch**.|
232| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt; | Yes   | Callback used to return the pinch event asynchronously. |
233
234  **Example**
235
236```js
237try {
238  inputMonitor.on('pinch', (pinchEvent) => {
239    console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
240    return false;
241  });
242} catch (error) {
243  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
244}
245```
246
247## inputMonitor.off('pinch')<sup>10+</sup>
248
249off(type: 'pinch', receiver?: Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt;): void
250
251Disables listening for global touchpad pinch events.
252
253**Required permissions**: ohos.permission.INPUT_MONITORING
254
255**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
256
257**Parameters**
258
259| Name      | Type                        | Mandatory  | Description                 |
260| -------- | -------------------------- | ---- | ------------------- |
261| type     | string                     | Yes   | Event type. This field has a fixed value of **pinch**.|
262| 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.|
263
264**Example**
265
266```js
267// Disable listening for a single callback.
268import { Pinch } from '@ohos.multimodalInput.gestureEvent';
269
270let callback = (pinchEvent: Pinch) => {
271  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
272  return false;
273};
274try {
275  inputMonitor.on('pinch', callback);
276  inputMonitor.off('pinch', callback);
277  console.log(`Monitor off success`);
278} catch (error) {
279  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
280}
281```
282
283```js
284// Cancel listening for all callbacks.
285import { Pinch } from '@ohos.multimodalInput.gestureEvent';
286
287let callback = (pinchEvent: Pinch) => {
288  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
289  return false;
290};
291try {
292  inputMonitor.on('pinch', callback);
293  inputMonitor.off('pinch');
294  console.log(`Monitor off success`);
295} catch (error) {
296  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
297}
298```
299
300## inputMonitor.on('threeFingersSwipe')<sup>10+</sup>
301
302on(type: 'threeFingersSwipe', receiver: Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt;): void
303
304Enables listening for three-finger swipe events.
305
306**Required permissions**: ohos.permission.INPUT_MONITORING
307
308**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
309
310**Parameters**
311
312| Name      | Type                        | Mandatory  | Description                 |
313| -------- | -------------------------- | ---- | ------------------- |
314| type     | string                     | Yes   | Event type. This field has a fixed value of **threeFingersSwipe**.|
315| receiver | Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt; | Yes   | Callback used to return the three-finger swipe event asynchronously. |
316
317  **Example**
318
319```js
320try {
321  inputMonitor.on('threeFingersSwipe', (threeFingersSwipe) => {
322    console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
323    return false;
324  });
325} catch (error) {
326  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
327}
328```
329
330## inputMonitor.off('threeFingersSwipe')<sup>10+</sup>
331
332off(type: 'threeFingersSwipe', receiver?: Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt;): void
333
334Disables listening for three-finger swipe events.
335
336**Required permissions**: ohos.permission.INPUT_MONITORING
337
338**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
339
340**Parameters**
341
342| Name      | Type                        | Mandatory  | Description                 |
343| -------- | -------------------------- | ---- | ------------------- |
344| type     | string                     | Yes   | Event type. This field has a fixed value of **threeFingersSwipe**.|
345| 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.|
346
347**Example**
348
349```js
350// Disable listening for a single callback.
351import { ThreeFingersSwipe } from '@ohos.multimodalInput.gestureEvent';
352
353let callback = (threeFingersSwipe: ThreeFingersSwipe) => {
354  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
355  return false;
356};
357try {
358  inputMonitor.on('threeFingersSwipe', callback);
359  inputMonitor.off("threeFingersSwipe", callback);
360  console.log(`Monitor off success`);
361} catch (error) {
362  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
363}
364```
365
366```js
367// Cancel listening for all callbacks.
368import { ThreeFingersSwipe } from '@ohos.multimodalInput.gestureEvent';
369
370let callback = (threeFingersSwipe: ThreeFingersSwipe) => {
371  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
372  return false;
373};
374try {
375  inputMonitor.on("threeFingersSwipe", callback);
376  inputMonitor.off("threeFingersSwipe");
377  console.log(`Monitor off success`);
378} catch (error) {
379  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
380}
381```
382
383## inputMonitor.on('fourFingersSwipe')<sup>10+</sup>
384
385on(type: 'fourFingersSwipe', receiver: Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt;): void
386
387Enables listening for four-finger swipe events.
388
389**Required permissions**: ohos.permission.INPUT_MONITORING
390
391**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
392
393**Parameters**
394
395| Name      | Type                        | Mandatory  | Description                 |
396| -------- | -------------------------- | ---- | ------------------- |
397| type     | string                     | Yes   | Event type. This field has a fixed value of **fourFingersSwipe**.|
398| receiver | Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt; | Yes   | Callback used to return the four-finger swipe event asynchronously. |
399
400  **Example**
401
402```js
403try {
404  inputMonitor.on('fourFingersSwipe', (fourFingersSwipe) => {
405    console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
406    return false;
407  });
408} catch (error) {
409  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
410}
411```
412
413## inputMonitor.off('fourFingersSwipe')<sup>10+</sup>
414
415off(type: 'fourFingersSwipe', receiver?: Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt;): void
416
417Disables listening for four-finger swipe events.
418
419**Required permissions**: ohos.permission.INPUT_MONITORING
420
421**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor
422
423**Parameters**
424
425| Name      | Type                        | Mandatory  | Description                 |
426| -------- | -------------------------- | ---- | ------------------- |
427| type     | string                     | Yes   | Event type. This field has a fixed value of **fourFingersSwipe**.|
428| 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.|
429
430**Example**
431
432```js
433// Disable listening for a single callback.
434import { FourFingersSwipe } from '@ohos.multimodalInput.gestureEvent';
435
436let callback = (fourFingersSwipe: FourFingersSwipe) => {
437  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
438  return false;
439};
440try {
441  inputMonitor.on('fourFingersSwipe', callback);
442  inputMonitor.off('fourFingersSwipe', callback);
443  console.log(`Monitor off success`);
444} catch (error) {
445  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
446}
447```
448
449```js
450// Cancel listening for all callbacks.
451import { FourFingersSwipe } from '@ohos.multimodalInput.gestureEvent';
452
453let callback = (fourFingersSwipe: FourFingersSwipe) => {
454  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
455  return false;
456};
457try {
458  inputMonitor.on('fourFingersSwipe', callback);
459  inputMonitor.off('fourFingersSwipe');
460  console.log(`Monitor off success`);
461} catch (error) {
462  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
463}
464```
465