• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputMonitor (输入监听)
2
3输入监听模块,提供了监听输入设备事件的能力。输入设备事件当前包括触摸(触屏)事件、鼠标输入事件和触控板输入事件。
4
5>**说明:**
6>
7>- 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9>- 本模块接口均为系统接口。
10>
11>- 文档中“全局”表示整个触控屏或触控板。如监听全局触摸事件,表示触摸触控板任何位置时,整个触控板的触摸事件均被监听。
12
13## 导入模块
14
15```js
16import inputMonitor from '@ohos.multimodalInput.inputMonitor';
17```
18
19## inputMonitor.on('touch')
20
21on(type: 'touch', receiver: TouchEventReceiver): void
22
23监听全局触摸(触屏)事件。
24
25**需要权限:** ohos.permission.INPUT_MONITORING
26
27**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
28
29**参数:**
30
31| 参数名       | 类型                                       | 必填   | 说明                  |
32| -------- | ---------------------------------------- | ---- | ------------------- |
33| type     | string                                   | 是    | 输入设备事件类型,取值'touch'。 |
34| receiver | [TouchEventReceiver](#toucheventreceiver) | 是    | 回调函数,异步上报触摸屏输入事件。 |
35
36**示例:**
37
38```js
39import { TouchEvent } from '@ohos.multimodalInput.touchEvent';
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
54监听全局鼠标事件。
55
56**需要权限:** ohos.permission.INPUT_MONITORING
57
58**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
59
60**参数:**
61
62| 参数名       | 类型                         | 必填   | 说明                  |
63| -------- | -------------------------- | ---- | ------------------- |
64| type     | string                     | 是    | 输入设备事件类型,取值'mouse'。 |
65| receiver | Callback&lt;MouseEvent&gt; | 是    | 回调函数,异步上报鼠标输入事件。  |
66
67  **示例:**
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## inputMonitor.off('touch')
83
84off(type: 'touch', receiver?: TouchEventReceiver): void
85
86取消监听全局触摸(触屏)事件。
87
88**需要权限:** ohos.permission.INPUT_MONITORING
89
90**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
91
92**参数:**
93
94| 参数名       | 类型                                       | 必填   | 说明                  |
95| -------- | ---------------------------------------- | ---- | ------------------- |
96| type     | string                                   | 是    | 输入设备事件类型,取值'touch'。 |
97| receiver | [TouchEventReceiver](#toucheventreceiver) | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。  |
98
99**示例:**
100
101```js
102import { TouchEvent } from '@ohos.multimodalInput.touchEvent';
103// 取消监听单个回调函数
104let callback = (touchEvent: TouchEvent) => {
105  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
106  return false;
107};
108try {
109  inputMonitor.on('touch', callback);
110  inputMonitor.off('touch', callback);
111  console.log(`Monitor off success`);
112} catch (error) {
113  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
114}
115```
116
117```js
118import { TouchEvent } from '@ohos.multimodalInput.touchEvent';
119// 取消监听所有回调函数
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
137取消监听全局鼠标事件。
138
139**需要权限:** ohos.permission.INPUT_MONITORING
140
141**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
142
143**参数:**
144
145| 参数名       | 类型                         | 必填   | 说明                  |
146| -------- | -------------------------- | ---- | ------------------- |
147| type     | string                     | 是    | 输入设备事件类型,取值'mouse'。 |
148| receiver | Callback&lt;MouseEvent&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
149
150**示例:**
151
152```js
153import { MouseEvent } from '@ohos.multimodalInput.mouseEvent';
154// 取消监听单个回调函数
155let callback = (mouseEvent: MouseEvent) => {
156  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
157  return false;
158};
159try {
160  inputMonitor.on('mouse', callback);
161  inputMonitor.off('mouse', callback);
162  console.log(`Monitor off success`);
163} catch (error) {
164  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
165}
166```
167
168```js
169import { MouseEvent } from '@ohos.multimodalInput.mouseEvent';
170// 取消监听所有回调函数
171let callback = (mouseEvent: MouseEvent) => {
172  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
173  return false;
174};
175try {
176  inputMonitor.on('mouse', callback);
177  inputMonitor.off('mouse');
178  console.log(`Monitor off success`);
179} catch (error) {
180  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
181}
182```
183
184## TouchEventReceiver
185
186触摸(触屏)输入事件的回调函数。
187
188**需要权限:** ohos.permission.INPUT_MONITORING
189
190**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
191
192**参数:**
193
194| 参数         | 类型                                       | 必填   | 说明                                       |
195| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
196| touchEvent | [TouchEvent](./js-apis-touchevent.md) | 是    | 触摸输入事件。 |
197
198**返回值:**
199
200| 类型      | 说明                                       |
201| ------- | ---------------------------------------- |
202| Boolean | 若返回true,本次触摸后续产生的事件不再分发到窗口;若返回false,本次触摸后续产生的事件还会分发到窗口。 |
203
204**示例:**
205
206```js
207import { TouchEvent } from '@ohos.multimodalInput.touchEvent';
208try {
209  inputMonitor.on('touch', touchEvent => {
210    if (touchEvent.touches.length == 3) { // 当前有三个手指按下
211      return true;
212    }
213    return false;
214  });
215} catch (error) {
216    console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
217}
218```
219
220## inputMonitor.on('pinch')<sup>10+</sup>
221
222on(type: 'pinch', receiver: Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt;): void
223
224监听全局触控板的捏合事件。
225
226**需要权限:** ohos.permission.INPUT_MONITORING
227
228**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
229
230**参数:**
231
232| 参数名       | 类型                         | 必填   | 说明                  |
233| -------- | -------------------------- | ---- | ------------------- |
234| type     | string                     | 是    | 输入设备事件类型,取值'pinch'。 |
235| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt; | 是    | 回调函数,异步上报捏合输入事件。  |
236
237  **示例:**
238
239```js
240import type { Pinch } from '@ohos.multimodalInput.gestureEvent';
241try {
242  inputMonitor.on('pinch', (pinchEvent) => {
243    console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
244    return false;
245  });
246} catch (error) {
247  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
248}
249```
250
251## inputMonitor.off('pinch')<sup>10+</sup>
252
253off(type: 'pinch', receiver?: Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt;): void
254
255取消监听全局触控板的捏合事件。
256
257**需要权限:** ohos.permission.INPUT_MONITORING
258
259**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
260
261**参数:**
262
263| 参数名       | 类型                         | 必填   | 说明                  |
264| -------- | -------------------------- | ---- | ------------------- |
265| type     | string                     | 是    | 输入设备事件类型,取值'pinch'。 |
266| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
267
268**示例:**
269
270```js
271// 取消监听单个回调函数
272import { Pinch } from '@ohos.multimodalInput.gestureEvent';
273
274let callback = (pinchEvent: Pinch) => {
275  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
276  return false;
277};
278try {
279  inputMonitor.on('pinch', callback);
280  inputMonitor.off('pinch', callback);
281  console.log(`Monitor off success`);
282} catch (error) {
283  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
284}
285```
286
287```js
288// 取消监听所有回调函数
289import { Pinch } from '@ohos.multimodalInput.gestureEvent';
290
291let callback = (pinchEvent: Pinch) => {
292  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
293  return false;
294};
295try {
296  inputMonitor.on('pinch', callback);
297  inputMonitor.off('pinch');
298  console.log(`Monitor off success`);
299} catch (error) {
300  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
301}
302```
303
304## inputMonitor.on('threeFingersSwipe')<sup>10+</sup>
305
306on(type: 'threeFingersSwipe', receiver: Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt;): void
307
308监听全局触控板的三指滑动事件。
309
310**需要权限:** ohos.permission.INPUT_MONITORING
311
312**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
313
314**参数:**
315
316| 参数名       | 类型                         | 必填   | 说明                  |
317| -------- | -------------------------- | ---- | ------------------- |
318| type     | string                     | 是    | 输入设备事件类型,取值'threeFingersSwipe'。 |
319| receiver | Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt; | 是    | 回调函数,异步上报三指滑动输入事件。  |
320
321  **示例:**
322
323```js
324try {
325  inputMonitor.on('threeFingersSwipe', (threeFingersSwipe) => {
326    console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
327    return false;
328  });
329} catch (error) {
330  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
331}
332```
333
334## inputMonitor.off('threeFingersSwipe')<sup>10+</sup>
335
336off(type: 'threeFingersSwipe', receiver?: Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt;): void
337
338取消监听全局触控板的三指滑动事件。
339
340**需要权限:** ohos.permission.INPUT_MONITORING
341
342**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
343
344**参数:**
345
346| 参数名       | 类型                         | 必填   | 说明                  |
347| -------- | -------------------------- | ---- | ------------------- |
348| type     | string                     | 是    | 输入设备事件类型,取值'threeFingersSwipe'。 |
349| receiver | Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
350
351**示例:**
352
353```js
354// 取消监听单个回调函数
355import { ThreeFingersSwipe } from '@ohos.multimodalInput.gestureEvent';
356
357let callback = (threeFingersSwipe: ThreeFingersSwipe) => {
358  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
359  return false;
360};
361try {
362  inputMonitor.on('threeFingersSwipe', callback);
363  inputMonitor.off("threeFingersSwipe", callback);
364  console.log(`Monitor off success`);
365} catch (error) {
366  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
367}
368```
369
370```js
371// 取消监听所有回调函数
372import { ThreeFingersSwipe } from '@ohos.multimodalInput.gestureEvent';
373
374let callback = (threeFingersSwipe: ThreeFingersSwipe) => {
375  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
376  return false;
377};
378try {
379  inputMonitor.on("threeFingersSwipe", callback);
380  inputMonitor.off("threeFingersSwipe");
381  console.log(`Monitor off success`);
382} catch (error) {
383  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
384}
385```
386
387## inputMonitor.on('fourFingersSwipe')<sup>10+</sup>
388
389on(type: 'fourFingersSwipe', receiver: Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt;): void
390
391监听全局触控板的四指滑动事件。
392
393**需要权限:** ohos.permission.INPUT_MONITORING
394
395**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
396
397**参数:**
398
399| 参数名       | 类型                         | 必填   | 说明                  |
400| -------- | -------------------------- | ---- | ------------------- |
401| type     | string                     | 是    | 输入设备事件类型,取值'fourFingersSwipe'。 |
402| receiver | Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt; | 是    | 回调函数,异步上报四指滑动输入事件。  |
403
404  **示例:**
405
406```js
407try {
408  inputMonitor.on('fourFingersSwipe', (fourFingersSwipe) => {
409    console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
410    return false;
411  });
412} catch (error) {
413  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
414}
415```
416
417## inputMonitor.off('fourFingersSwipe')<sup>10+</sup>
418
419off(type: 'fourFingersSwipe', receiver?: Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt;): void
420
421取消监听全局触控板的四指滑动事件。
422
423**需要权限:** ohos.permission.INPUT_MONITORING
424
425**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
426
427**参数:**
428
429| 参数名       | 类型                         | 必填   | 说明                  |
430| -------- | -------------------------- | ---- | ------------------- |
431| type     | string                     | 是    | 输入设备事件类型,取值'fourFingersSwipe'。 |
432| receiver | Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
433
434**示例:**
435
436```js
437// 取消监听单个回调函数
438import { FourFingersSwipe } from '@ohos.multimodalInput.gestureEvent';
439
440let callback = (fourFingersSwipe: FourFingersSwipe) => {
441  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
442  return false;
443};
444try {
445  inputMonitor.on('fourFingersSwipe', callback);
446  inputMonitor.off('fourFingersSwipe', callback);
447  console.log(`Monitor off success`);
448} catch (error) {
449  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
450}
451```
452
453```js
454// 取消监听所有回调函数
455import { FourFingersSwipe } from '@ohos.multimodalInput.gestureEvent';
456
457let callback = (fourFingersSwipe: FourFingersSwipe) => {
458  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
459  return false;
460};
461try {
462  inputMonitor.on('fourFingersSwipe', callback);
463  inputMonitor.off('fourFingersSwipe');
464  console.log(`Monitor off success`);
465} catch (error) {
466  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
467}
468```
469