• 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 '@kit.InputKit';
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以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
39
40| 错误码ID  | 错误信息             |
41| ---- | --------------------- |
42| 201  | Permission denied.   |
43| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
44
45**示例:**
46
47```js
48import { TouchEvent } from '@kit.InputKit';
49try {
50  inputMonitor.on('touch', (touchEvent: TouchEvent) => {
51    console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
52    return false;
53  });
54} catch (error) {
55  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
56}
57```
58
59## inputMonitor.on('mouse')<sup>9+</sup>
60
61on(type: 'mouse', receiver: Callback&lt;MouseEvent&gt;): void
62
63监听全局鼠标事件。
64
65**需要权限:** ohos.permission.INPUT_MONITORING
66
67**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
68
69**参数:**
70
71| 参数名       | 类型                         | 必填   | 说明                  |
72| -------- | -------------------------- | ---- | ------------------- |
73| type     | string                     | 是    | 输入设备事件类型,取值'mouse'。 |
74| receiver | Callback&lt;[MouseEvent](js-apis-mouseevent.md#mouseevent)&gt; | 是    | 回调函数,异步上报鼠标输入事件。  |
75
76**错误码**:
77
78以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
79
80| 错误码ID  | 错误信息             |
81| ---- | --------------------- |
82| 201  | Permission denied.   |
83| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
84
85**示例:**
86
87```js
88import { MouseEvent } from '@kit.InputKit';
89
90try {
91  inputMonitor.on('mouse', (mouseEvent: MouseEvent) => {
92    console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
93    return false;
94  });
95} catch (error) {
96  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
97}
98```
99
100## inputMonitor.on('mouse')<sup>11+</sup>
101
102on(type: 'mouse', rect: display.Rect[], receiver: Callback&lt;MouseEvent&gt;): void
103
104监听鼠标事件,当鼠标移动至指定矩形区域内时,触发回调任务。
105
106**需要权限:** ohos.permission.INPUT_MONITORING
107
108**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
109
110**参数:**
111
112| 参数名       | 类型                         | 必填   | 说明                  |
113| -------- | -------------------------- | ---- | ------------------- |
114| type     | string                     | 是    | 输入设备事件类型,取值'mouse'。 |
115| rect     | display.Rect[]             | 是    | 可以触发回调任务的矩形区域,可传入1至2个。 |
116| receiver | Callback&lt;[MouseEvent](js-apis-mouseevent.md#mouseevent)&gt; | 是    | 回调函数,异步上报鼠标输入事件。  |
117
118**错误码**:
119
120以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
121
122| 错误码ID  | 错误信息             |
123| ---- | --------------------- |
124| 201  | Permission denied.   |
125| 202  | SystemAPI permission error.  |
126| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
127
128**示例:**
129
130```js
131import { MouseEvent } from '@kit.InputKit';
132import { display } from '@kit.ArkUI';
133
134/**
135 * 鼠标在矩形区域内时,触发的回调任务。
136 */
137let callback = (mouseEvent : MouseEvent) => {
138  this.getUIContext().getPromptAction().showToast({
139    message: `监听成功:${JSON.stringify(mouseEvent)}`
140  })
141  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
142  return false;
143};
144
145/**
146 * 触发回调事件矩形区域。
147 */
148let rect: display.Rect[] = [{
149  left: 100,
150  top: 100,
151  width: 100,
152  height: 100
153}, {
154  left: 600,
155  top: 100,
156  width: 100,
157  height: 100
158}];
159
160try {
161  inputMonitor.on('mouse', rect, callback);
162} catch (error) {
163  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
164}
165```
166
167## inputMonitor.off('touch')
168
169off(type: 'touch', receiver?: TouchEventReceiver): void
170
171取消监听全局触摸(触屏)事件。
172
173**需要权限:** ohos.permission.INPUT_MONITORING
174
175**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
176
177**参数:**
178
179| 参数名       | 类型                                       | 必填   | 说明                  |
180| -------- | ---------------------------------------- | ---- | ------------------- |
181| type     | string                                   | 是    | 输入设备事件类型,取值'touch'。 |
182| receiver | [TouchEventReceiver](#toucheventreceiver) | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。  |
183
184**错误码**:
185
186以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
187
188| 错误码ID  | 错误信息             |
189| ---- | --------------------- |
190| 201  | Permission denied.   |
191| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
192
193**示例:**
194
195```js
196import { TouchEvent } from '@kit.InputKit';
197// 取消监听单个回调函数
198let callback = (touchEvent: TouchEvent) => {
199  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
200  return false;
201};
202try {
203  inputMonitor.on('touch', callback);
204  inputMonitor.off('touch', callback);
205  console.log(`Monitor off success`);
206} catch (error) {
207  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
208}
209```
210
211```js
212import { TouchEvent } from '@kit.InputKit';
213// 取消监听所有回调函数
214let callback = (touchEvent: TouchEvent) => {
215  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
216  return false;
217};
218try {
219  inputMonitor.on('touch', callback);
220  inputMonitor.off('touch');
221  console.log(`Monitor off success`);
222} catch (error) {
223  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
224}
225```
226
227## inputMonitor.off('mouse')<sup>9+</sup>
228
229off(type: 'mouse', receiver?: Callback&lt;MouseEvent&gt;): void
230
231取消监听全局鼠标事件。
232
233**需要权限:** ohos.permission.INPUT_MONITORING
234
235**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
236
237**参数:**
238
239| 参数名       | 类型                         | 必填   | 说明                  |
240| -------- | -------------------------- | ---- | ------------------- |
241| type     | string                     | 是    | 输入设备事件类型,取值'mouse'。 |
242| receiver | Callback&lt;MouseEvent&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
243
244**错误码**:
245
246以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
247
248| 错误码ID  | 错误信息             |
249| ---- | --------------------- |
250| 201  | Permission denied.   |
251| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
252
253**示例:**
254
255```js
256import { MouseEvent } from '@kit.InputKit';
257// 取消监听单个回调函数
258let callback = (mouseEvent: MouseEvent) => {
259  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
260  return false;
261};
262try {
263  inputMonitor.on('mouse', callback);
264  inputMonitor.off('mouse', callback);
265  console.log(`Monitor off success`);
266} catch (error) {
267  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
268}
269```
270
271```js
272import { MouseEvent } from '@kit.InputKit';
273// 取消监听所有回调函数
274let callback = (mouseEvent: MouseEvent) => {
275  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
276  return false;
277};
278try {
279  inputMonitor.on('mouse', callback);
280  inputMonitor.off('mouse');
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## TouchEventReceiver
288
289(touchEvent: TouchEvent): Boolean
290
291触摸(触屏)输入事件的回调函数。
292
293**需要权限:** ohos.permission.INPUT_MONITORING
294
295**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
296
297**参数:**
298
299| 参数名         | 类型                                       | 必填   | 说明                                       |
300| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
301| touchEvent | [TouchEvent](js-apis-touchevent.md#touchevent) | 是    | 触摸输入事件。 |
302
303**返回值:**
304
305| 类型      | 说明                                       |
306| ------- | ---------------------------------------- |
307| Boolean | 若返回true,本次触摸后续产生的事件不再分发到窗口;若返回false,本次触摸后续产生的事件还会分发到窗口。 |
308
309**示例:**
310
311```js
312import { TouchEvent } from '@kit.InputKit';
313try {
314  inputMonitor.on('touch', touchEvent => {
315    if (touchEvent.touches.length == 3) { // 当前有三个手指按下
316      return true;
317    }
318    return false;
319  });
320} catch (error) {
321    console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
322}
323```
324
325## inputMonitor.on('pinch')<sup>10+</sup>
326
327on(type: 'pinch', receiver: Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt;): void
328
329监听全局触控板的捏合事件。
330
331**需要权限:** ohos.permission.INPUT_MONITORING
332
333**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
334
335**参数:**
336
337| 参数名       | 类型                         | 必填   | 说明                  |
338| -------- | -------------------------- | ---- | ------------------- |
339| type     | string                     | 是    | 输入设备事件类型,取值'pinch'。 |
340| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt; | 是    | 回调函数,异步上报捏合输入事件。  |
341
342**错误码**:
343
344以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
345
346| 错误码ID  | 错误信息             |
347| ---- | --------------------- |
348| 201  | Permission denied.   |
349| 202  | SystemAPI permission error.  |
350| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
351
352**示例:**
353
354```js
355import type { Pinch } from '@kit.InputKit';
356try {
357  inputMonitor.on('pinch', (pinchEvent) => {
358    console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
359    return false;
360  });
361} catch (error) {
362  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
363}
364```
365
366## inputMonitor.off('pinch')<sup>10+</sup>
367
368off(type: 'pinch', receiver?: Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt;): void
369
370取消监听全局触控板的捏合事件。
371
372**需要权限:** ohos.permission.INPUT_MONITORING
373
374**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
375
376**参数:**
377
378| 参数名       | 类型                         | 必填   | 说明                  |
379| -------- | -------------------------- | ---- | ------------------- |
380| type     | string                     | 是    | 输入设备事件类型,取值'pinch'。 |
381| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
382
383**错误码**:
384
385以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
386
387| 错误码ID  | 错误信息             |
388| ---- | --------------------- |
389| 201  | Permission denied.   |
390| 202  | SystemAPI permission error.  |
391| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
392
393**示例:**
394
395```js
396// 取消监听单个回调函数
397import { Pinch } from '@kit.InputKit';
398
399let callback = (pinchEvent: Pinch) => {
400  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
401  return false;
402};
403try {
404  inputMonitor.on('pinch', callback);
405  inputMonitor.off('pinch', callback);
406  console.log(`Monitor off success`);
407} catch (error) {
408  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
409}
410```
411
412```js
413// 取消监听所有回调函数
414import { Pinch } from '@kit.InputKit';
415
416let callback = (pinchEvent: Pinch) => {
417  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
418  return false;
419};
420try {
421  inputMonitor.on('pinch', callback);
422  inputMonitor.off('pinch');
423  console.log(`Monitor off success`);
424} catch (error) {
425  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
426}
427```
428
429## inputMonitor.on('threeFingersSwipe')<sup>10+</sup>
430
431on(type: 'threeFingersSwipe', receiver: Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt;): void
432
433监听全局触控板的三指滑动事件。
434
435**需要权限:** ohos.permission.INPUT_MONITORING
436
437**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
438
439**参数:**
440
441| 参数名       | 类型                         | 必填   | 说明                  |
442| -------- | -------------------------- | ---- | ------------------- |
443| type     | string                     | 是    | 输入设备事件类型,取值'threeFingersSwipe'。 |
444| receiver | Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt; | 是    | 回调函数,异步上报三指滑动输入事件。  |
445
446**错误码**:
447
448以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
449
450| 错误码ID  | 错误信息             |
451| ---- | --------------------- |
452| 201  | Permission denied.   |
453| 202  | SystemAPI permission error.  |
454| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
455
456**示例:**
457
458```js
459try {
460  inputMonitor.on('threeFingersSwipe', (threeFingersSwipe) => {
461    console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
462    return false;
463  });
464} catch (error) {
465  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
466}
467```
468
469## inputMonitor.off('threeFingersSwipe')<sup>10+</sup>
470
471off(type: 'threeFingersSwipe', receiver?: Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt;): void
472
473取消监听全局触控板的三指滑动事件。
474
475**需要权限:** ohos.permission.INPUT_MONITORING
476
477**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
478
479**参数:**
480
481| 参数名       | 类型                         | 必填   | 说明                  |
482| -------- | -------------------------- | ---- | ------------------- |
483| type     | string                     | 是    | 输入设备事件类型,取值'threeFingersSwipe'。 |
484| receiver | Callback&lt;[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
485
486**错误码**:
487
488以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
489
490| 错误码ID  | 错误信息             |
491| ---- | --------------------- |
492| 201  | Permission denied.   |
493| 202  | SystemAPI permission error.  |
494| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
495
496**示例:**
497
498```js
499// 取消监听单个回调函数
500import { ThreeFingersSwipe } from '@kit.InputKit';
501
502let callback = (threeFingersSwipe: ThreeFingersSwipe) => {
503  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
504  return false;
505};
506try {
507  inputMonitor.on('threeFingersSwipe', callback);
508  inputMonitor.off("threeFingersSwipe", callback);
509  console.log(`Monitor off success`);
510} catch (error) {
511  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
512}
513```
514
515```js
516// 取消监听所有回调函数
517import { ThreeFingersSwipe } from '@kit.InputKit';
518
519let callback = (threeFingersSwipe: ThreeFingersSwipe) => {
520  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
521  return false;
522};
523try {
524  inputMonitor.on("threeFingersSwipe", callback);
525  inputMonitor.off("threeFingersSwipe");
526  console.log(`Monitor off success`);
527} catch (error) {
528  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
529}
530```
531
532## inputMonitor.on('fourFingersSwipe')<sup>10+</sup>
533
534on(type: 'fourFingersSwipe', receiver: Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt;): void
535
536监听全局触控板的四指滑动事件。
537
538**需要权限:** ohos.permission.INPUT_MONITORING
539
540**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
541
542**参数:**
543
544| 参数名       | 类型                         | 必填   | 说明                  |
545| -------- | -------------------------- | ---- | ------------------- |
546| type     | string                     | 是    | 输入设备事件类型,取值'fourFingersSwipe'。 |
547| receiver | Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt; | 是    | 回调函数,异步上报四指滑动输入事件。  |
548
549**错误码**:
550
551以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
552
553| 错误码ID  | 错误信息             |
554| ---- | --------------------- |
555| 201  | Permission denied.   |
556| 202  | SystemAPI permission error.  |
557| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
558
559**示例:**
560
561```js
562try {
563  inputMonitor.on('fourFingersSwipe', (fourFingersSwipe) => {
564    console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
565    return false;
566  });
567} catch (error) {
568  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
569}
570```
571
572## inputMonitor.off('fourFingersSwipe')<sup>10+</sup>
573
574off(type: 'fourFingersSwipe', receiver?: Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt;): void
575
576取消监听全局触控板的四指滑动事件。
577
578**需要权限:** ohos.permission.INPUT_MONITORING
579
580**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
581
582**参数:**
583
584| 参数名       | 类型                         | 必填   | 说明                  |
585| -------- | -------------------------- | ---- | ------------------- |
586| type     | string                     | 是    | 输入设备事件类型,取值'fourFingersSwipe'。 |
587| receiver | Callback&lt;[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
588
589**错误码**:
590
591以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
592
593| 错误码ID  | 错误信息             |
594| ---- | --------------------- |
595| 201  | Permission denied.   |
596| 202  | SystemAPI permission error.  |
597| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
598
599**示例:**
600
601```js
602// 取消监听单个回调函数
603import { FourFingersSwipe } from '@kit.InputKit';
604
605let callback = (fourFingersSwipe: FourFingersSwipe) => {
606  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
607  return false;
608};
609try {
610  inputMonitor.on('fourFingersSwipe', callback);
611  inputMonitor.off('fourFingersSwipe', callback);
612  console.log(`Monitor off success`);
613} catch (error) {
614  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
615}
616```
617
618```js
619// 取消监听所有回调函数
620import { FourFingersSwipe } from '@kit.InputKit';
621
622let callback = (fourFingersSwipe: FourFingersSwipe) => {
623  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
624  return false;
625};
626try {
627  inputMonitor.on('fourFingersSwipe', callback);
628  inputMonitor.off('fourFingersSwipe');
629  console.log(`Monitor off success`);
630} catch (error) {
631  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
632}
633```
634
635## inputMonitor.on('rotate')<sup>11+</sup>
636
637on(type: 'rotate', fingers: number, receiver: Callback&lt;Rotate&gt;): void
638
639监听全局触控板的旋转事件。
640
641**需要权限:** ohos.permission.INPUT_MONITORING
642
643**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
644
645**参数:**
646
647| 参数名       | 类型                         | 必填   | 说明                  |
648| -------- | -------------------------- | ---- | ------------------- |
649| type     | string                     | 是    | 输入设备事件类型,取值'rotate'。 |
650| fingers     | number                     | 是    | 旋转的手指数,目前支持监听手指数是2。 |
651| receiver | Callback&lt;[Rotate](js-apis-multimodalinput-gestureevent.md#rotate)&gt; | 是    | 回调函数,异步上报旋转输入事件。  |
652
653**错误码**:
654
655以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
656
657| 错误码ID  | 错误信息             |
658| ---- | --------------------- |
659| 201  | Permission denied.   |
660| 202  | SystemAPI permission error.  |
661| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
662
663**示例:**
664
665```js
666import type { Rotate } from '@kit.InputKit';
667try {
668  inputMonitor.on('rotate', 2, (rotateEvent: Rotate) => {
669    console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`);
670    return false;
671  });
672} catch (error) {
673  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
674}
675```
676
677## inputMonitor.off('rotate')<sup>11+</sup>
678
679off(type: 'rotate', fingers: number, receiver?: Callback&lt;Rotate&gt;): void
680
681取消监听全局触控板的旋转事件。
682
683**需要权限:** ohos.permission.INPUT_MONITORING
684
685**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
686
687**参数:**
688
689| 参数名       | 类型                         | 必填   | 说明                  |
690| -------- | -------------------------- | ---- | ------------------- |
691| type     | string                     | 是    | 输入设备事件类型,取值'rotate'。 |
692| fingers     | number                     | 是    | 旋转的手指数,目前支持监听手指数是2。 |
693| receiver | Callback&lt;[Rotate](js-apis-multimodalinput-gestureevent.md#rotate)&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
694
695**错误码**:
696
697以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
698
699| 错误码ID  | 错误信息             |
700| ---- | --------------------- |
701| 201  | Permission denied.   |
702| 202  | SystemAPI permission error.  |
703| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
704
705**示例:**
706
707```js
708// 取消监听单个回调函数
709import { Rotate } from '@kit.InputKit';
710
711let callback = (rotateEvent: Rotate) => {
712  console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`);
713  return false;
714};
715try {
716  inputMonitor.on('rotate', 2, callback);
717  inputMonitor.off('rotate', 2, callback);
718  console.log(`Monitor off success`);
719} catch (error) {
720  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
721}
722```
723
724```js
725// 取消监听所有回调函数
726import { Rotate } from '@kit.InputKit';
727
728let callback = (rotateEvent: Rotate) => {
729  console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`);
730  return false;
731};
732try {
733  inputMonitor.on('rotate', 2, callback);
734  inputMonitor.off('rotate', 2);
735  console.log(`Monitor off success`);
736} catch (error) {
737  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
738}
739```
740
741## inputMonitor.on('pinch')<sup>11+</sup>
742
743on(type: 'pinch', fingers: number, receiver: Callback&lt;Pinch&gt;): void
744
745监听全局触控板的捏合事件。
746
747**需要权限:** ohos.permission.INPUT_MONITORING
748
749**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
750
751**参数:**
752
753| 参数名       | 类型                         | 必填   | 说明                  |
754| -------- | -------------------------- | ---- | ------------------- |
755| type     | string                     | 是    | 输入设备事件类型,取值'pinch'。 |
756| fingers     | number                     | 是    | 捏合的手指数,取值范围:大于等于2。 |
757| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt; | 是    | 回调函数,异步上报捏合输入事件。  |
758
759**错误码**:
760
761以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
762
763| 错误码ID  | 错误信息             |
764| ---- | --------------------- |
765| 201  | Permission denied.   |
766| 202  | SystemAPI permission error.  |
767| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
768
769**示例:**
770
771```js
772import type { Pinch } from '@kit.InputKit';
773try {
774  inputMonitor.on('pinch', 2, (pinchEvent: Pinch) => {
775    console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
776    return false;
777  });
778} catch (error) {
779  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
780}
781```
782
783## inputMonitor.off('pinch')<sup>11+</sup>
784
785off(type: 'pinch', fingers: number, receiver?: Callback&lt;Pinch&gt;): void
786
787取消监听全局触控板的捏合事件。
788
789**需要权限:** ohos.permission.INPUT_MONITORING
790
791**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
792
793**参数:**
794
795| 参数名       | 类型                         | 必填   | 说明                  |
796| -------- | -------------------------- | ---- | ------------------- |
797| type     | string                     | 是    | 输入设备事件类型,取值'pinch'。 |
798| fingers     | number                     | 是    | 捏合的手指数,取值范围:大于等于2。 |
799| receiver | Callback&lt;[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)&gt; | 否    | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
800
801**错误码**:
802
803以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
804
805| 错误码ID  | 错误信息             |
806| ---- | --------------------- |
807| 201  | Permission denied.   |
808| 202  | SystemAPI permission error.  |
809| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
810
811**示例:**
812
813```js
814// 取消监听单个回调函数
815import { Pinch } from '@kit.InputKit';
816
817let callback = (pinchEvent: Pinch) => {
818  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
819  return false;
820};
821try {
822  inputMonitor.on('pinch', 2, callback);
823  inputMonitor.off('pinch', 2, callback);
824  console.log(`Monitor off success`);
825} catch (error) {
826  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
827}
828```
829
830```js
831// 取消监听所有回调函数
832import { Pinch } from '@kit.InputKit';
833
834let callback = (pinchEvent: Pinch) => {
835  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
836  return false;
837};
838try {
839  inputMonitor.on('pinch', 2, callback);
840  inputMonitor.off('pinch', 2);
841  console.log(`Monitor off success`);
842} catch (error) {
843  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
844}
845```
846
847## inputMonitor.on('threeFingersTap')<sup>11+</sup>
848
849on(type: 'threeFingersTap', receiver: Callback&lt;[ThreeFingersTap](js-apis-multimodalinput-gestureevent.md#threefingerstap)&gt;): void
850
851监听全局触控板的三指轻点事件。
852
853**需要权限:** ohos.permission.INPUT_MONITORING
854
855**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
856
857**参数:**
858
859| 参数名   | 类型                                                         | 必填 | 说明                                      |
860| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- |
861| type     | string                                                       | 是   | 输入设备事件类型,取值'threeFingersTap'。 |
862| receiver | Callback&lt;[ThreeFingersTap](js-apis-multimodalinput-gestureevent.md#threefingerstap)&gt; | 是   | 回调函数,异步上报三指轻点输入事件。      |
863
864**错误码**:
865
866以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
867
868| 错误码ID  | 错误信息             |
869| ---- | --------------------- |
870| 201  | Permission denied.   |
871| 202  | SystemAPI permission error.  |
872| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
873
874**示例:**
875
876```js
877try {
878  inputMonitor.on('threeFingersTap', (threeFingersTap) => {
879    console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`);
880    return false;
881  });
882} catch (error) {
883  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
884}
885```
886
887## inputMonitor.off('threeFingersTap')<sup>11+</sup>
888
889off(type: 'threeFingersTap', receiver?: Callback&lt;[ThreeFingersTap](js-apis-multimodalinput-gestureevent.md#threefingerstap)&gt;): void
890
891取消监听全局触控板的三指轻点事件。
892
893**需要权限:** ohos.permission.INPUT_MONITORING
894
895**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
896
897**参数:**
898
899| 参数名   | 类型                                                         | 必填 | 说明                                                         |
900| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
901| type     | string                                                       | 是   | 输入设备事件类型,取值'threeFingersTap'。                    |
902| receiver | Callback&lt;[ThreeFingersTap](js-apis-multimodalinput-gestureevent.md#threefingerstap)&gt; | 否   | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
903
904**错误码**:
905
906以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
907
908| 错误码ID  | 错误信息             |
909| ---- | --------------------- |
910| 201  | Permission denied.   |
911| 202  | SystemAPI permission error.  |
912| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
913
914**示例:**
915
916```js
917// 取消监听单个回调函数
918import { ThreeFingersTap } from '@kit.InputKit';
919
920let callback = (threeFingersTap: ThreeFingersTap) => {
921  console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`);
922  return false;
923};
924try {
925  inputMonitor.on('threeFingersTap', callback);
926  inputMonitor.off("threeFingersTap", callback);
927  console.log(`Monitor off success`);
928} catch (error) {
929  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
930}
931```
932
933```js
934// 取消监听所有回调函数
935import { ThreeFingersTap } from '@kit.InputKit';
936
937let callback = (threeFingersTap: ThreeFingersTap) => {
938  console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`);
939  return false;
940};
941try {
942  inputMonitor.on('threeFingersTap', callback);
943  inputMonitor.off("threeFingersTap");
944  console.log(`Monitor off success`);
945} catch (error) {
946  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
947}
948```
949
950## inputMonitor.on('touchscreenSwipe')<sup>18+</sup>
951
952on(type: 'touchscreenSwipe', fingers: number, receiver: Callback&lt;TouchGestureEvent&gt;): void
953
954监听触摸屏滑动手势事件。
955
956**需要权限:** ohos.permission.INPUT_MONITORING
957
958**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
959
960**参数:**
961
962| 参数名   | 类型                                                         | 必填 | 说明                                                         |
963| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
964| type     | string                                                       | 是   | 输入设备事件类型,取值'touchscreenSwipe'。                    |
965| fingers  | number                                                       | 是   | 滑动手势的手指数,取值范围:[3,5]。 |
966| receiver | Callback&lt;[TouchGestureEvent](js-apis-multimodalinput-gestureevent-sys.md#touchgestureevent)&gt; | 是   | 回调函数,异步上报触摸屏滑动手势事件。 |
967
968**错误码**:
969
970以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
971
972| 错误码ID  | 错误信息             |
973| ---- | --------------------- |
974| 201  | Permission denied.   |
975| 202  | SystemAPI permission error.  |
976| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
977
978**示例:**
979
980```js
981import inputMonitor from '@ohos.multimodalInput.inputMonitor';
982import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';
983
984let fingers: number = 4;
985try {
986  inputMonitor.on('touchscreenSwipe', fingers, (event: TouchGestureEvent) => {
987    console.log(`Monitor on success ${JSON.stringify(event)}`);
988  });
989} catch (error) {
990  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
991}
992```
993
994## inputMonitor.off('touchscreenSwipe')<sup>18+</sup>
995
996off(type: 'touchscreenSwipe', fingers: number, receiver?: Callback&lt;TouchGestureEvent&gt;): void
997
998取消监听触摸屏滑动手势事件。
999
1000**需要权限:** ohos.permission.INPUT_MONITORING
1001
1002**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
1003
1004**参数:**
1005
1006| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1007| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1008| type     | string                                                       | 是   | 输入设备事件类型,取值'touchscreenSwipe'。                    |
1009| fingers  | number                                                       | 是   | 滑动手势的手指数,取值范围:[3,5]。 |
1010| receiver | Callback&lt;[TouchGestureEvent](js-apis-multimodalinput-gestureevent-sys.md#touchgestureevent)&gt; | 否   | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
1011
1012**错误码**:
1013
1014以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1015
1016| 错误码ID  | 错误信息             |
1017| ---- | --------------------- |
1018| 201  | Permission denied.   |
1019| 202  | SystemAPI permission error.  |
1020| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
1021
1022**示例:**
1023
1024```js
1025// 取消监听单个回调函数
1026import inputMonitor from '@ohos.multimodalInput.inputMonitor';
1027import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';
1028
1029let callback = (event: TouchGestureEvent) => {
1030  console.log(`Monitor on success ${JSON.stringify(event)}`);
1031};
1032let fingers: number = 4;
1033try {
1034  inputMonitor.on('touchscreenSwipe', fingers, callback);
1035  inputMonitor.off('touchscreenSwipe', fingers, callback);
1036} catch (error) {
1037  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
1038}
1039```
1040
1041```js
1042// 取消监听所有回调函数
1043import inputMonitor from '@ohos.multimodalInput.inputMonitor';
1044import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';
1045
1046let fingers: number = 4;
1047try {
1048  inputMonitor.on('touchscreenSwipe', fingers, (event: TouchGestureEvent) => {
1049    console.log(`Monitor on success ${JSON.stringify(event)}`);
1050  });
1051  inputMonitor.off('touchscreenSwipe', fingers);
1052} catch (error) {
1053  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
1054}
1055```
1056
1057## inputMonitor.on('touchscreenPinch')<sup>18+</sup>
1058
1059on(type: 'touchscreenPinch', fingers: number, receiver: Callback&lt;TouchGestureEvent&gt;): void
1060
1061监听触摸屏捏合手势事件。
1062
1063**需要权限:** ohos.permission.INPUT_MONITORING
1064
1065**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
1066
1067**参数:**
1068
1069| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1070| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1071| type     | string                                                       | 是   | 输入设备事件类型,取值'touchscreenPinch'。                    |
1072| fingers  | number                                                       | 是   | 捏合手势的手指数,取值范围:[4,5]。 |
1073| receiver | Callback&lt;[TouchGestureEvent](js-apis-multimodalinput-gestureevent-sys.md#touchgestureevent)&gt; | 是   | 回调函数,异步上报触摸屏捏合手势事件。 |
1074
1075**错误码**:
1076
1077以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1078
1079| 错误码ID  | 错误信息             |
1080| ---- | --------------------- |
1081| 201  | Permission denied.   |
1082| 202  | SystemAPI permission error.  |
1083| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
1084
1085**示例:**
1086
1087```js
1088import inputMonitor from '@ohos.multimodalInput.inputMonitor';
1089import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';
1090
1091let fingers: number = 4;
1092try {
1093  inputMonitor.on('touchscreenPinch', fingers, (event: TouchGestureEvent) => {
1094    console.log(`Monitor on success ${JSON.stringify(event)}`);
1095  });
1096} catch (error) {
1097  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
1098}
1099```
1100
1101## inputMonitor.off('touchscreenPinch')<sup>18+</sup>
1102
1103off(type: 'touchscreenPinch', fingers: number, receiver?: Callback&lt;TouchGestureEvent&gt;): void
1104
1105取消监听触摸屏捏合手势事件。
1106
1107**需要权限:** ohos.permission.INPUT_MONITORING
1108
1109**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
1110
1111**参数:**
1112
1113| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1114| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1115| type     | string                                                       | 是   | 输入设备事件类型,取值'touchscreenPinch'。                    |
1116| fingers  | number                                                       | 是   | 捏合手势的手指数,取值范围:[4,5]。 |
1117| receiver | Callback&lt;[TouchGestureEvent](js-apis-multimodalinput-gestureevent-sys.md#touchgestureevent)&gt; | 否   | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 |
1118
1119**错误码**:
1120
1121以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1122
1123| 错误码ID  | 错误信息             |
1124| ---- | --------------------- |
1125| 201  | Permission denied.   |
1126| 202  | SystemAPI permission error.  |
1127| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
1128
1129**示例:**
1130
1131```js
1132// 取消监听单个回调函数
1133import inputMonitor from '@ohos.multimodalInput.inputMonitor';
1134import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';
1135
1136let callback = (event: TouchGestureEvent) => {
1137  console.log(`Monitor on success ${JSON.stringify(event)}`);
1138};
1139let fingers: number = 4;
1140try {
1141  inputMonitor.on('touchscreenPinch', fingers, callback);
1142  inputMonitor.off("touchscreenPinch", fingers, callback);
1143} catch (error) {
1144  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
1145}
1146```
1147
1148```js
1149// 取消监听所有回调函数
1150import inputMonitor from '@ohos.multimodalInput.inputMonitor';
1151import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';
1152
1153let fingers: number = 4;
1154try {
1155  inputMonitor.on('touchscreenPinch', fingers, (event: TouchGestureEvent) => {
1156    console.log(`Monitor on success ${JSON.stringify(event)}`);
1157  });
1158  inputMonitor.off("touchscreenPinch", fingers);
1159} catch (error) {
1160  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
1161}
1162```
1163
1164## inputMonitor.on('keyPressed')<sup>15+</sup>
1165
1166on(type: 'keyPressed', keys: Array&lt;KeyCode&gt;, receiver: Callback&lt;KeyEvent&gt;): void
1167
1168监听指定按键的按下抬起事件,支持监听META_LEFT键、META_RIGHT键、电源键、音量键。
1169
1170**需要权限:** ohos.permission.INPUT_MONITORING
1171
1172**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
1173
1174**参数:**
1175
1176| 参数名   | 类型                                                        | 必填 | 说明                                 |
1177| -------- | ----------------------------------------------------------- | ---- | ------------------------------------ |
1178| type     | string                                                      | 是   | 按键事件类型,取唯一值'keyPressed'。 |
1179| keys     | Array<[KeyCode](js-apis-keycode.md#keycode)> | 是   | 按键码列表,支持如下取值:KEYCODE_META_LEFT、KEYCODE_META_RIGHT、KEYCODE_POWER、KEYCODE_VOLUME_DOWN、KEYCODE_VOLUME_UP。                      |
1180| receiver | Callback&lt;[KeyEvent](js-apis-keyevent.md#keyevent)&gt;    | 是   | 用于接收上报数据的回调函数。         |
1181
1182**错误码**:
1183
1184以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[按键监听错误码](./errorcode-inputkeymonitor.md)。
1185
1186| 错误码ID | 错误信息                                                     |
1187| -------- | ------------------------------------------------------------ |
1188| 201      | Permission denied.                                           |
1189| 202      | Permission denied, non-system app called system api.         |
1190| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
1191| 4100001  | Event listening not supported for the key.                   |
1192
1193**示例:**
1194
1195```js
1196import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit';
1197
1198let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP];
1199try {
1200  inputMonitor.on('keyPressed', keys, (event: KeyEvent ) => {
1201    console.log(`Monitor on success ${JSON.stringify(event)}`);
1202  });
1203} catch (error) {
1204  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
1205}
1206```
1207
1208## inputMonitor.off('keyPressed')<sup>15+</sup>
1209
1210off(type: 'keyPressed', receiver?: Callback&lt;KeyEvent&gt;): void
1211
1212取消监听按键按下抬起事件。支持取消监听META_LEFT键、META_RIGHT键、电源键、音量键。需和inputMonitor.on('keyPressed')配套使用。
1213
1214**需要权限:** ohos.permission.INPUT_MONITORING
1215
1216**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor
1217
1218**参数:**
1219
1220| 参数名   | 类型                                                      | 必填 | 说明                                                         |
1221| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
1222| type     | string                                                    | 是   | 按键事件类型,取唯一值'keyPressed'。                         |
1223| receiver | Callback&lt;[KeyEvent](js-apis-keyevent.md#keyevent)&gt; | 否   | 需要取消监听的回调函数。若不填,取消应用所有按键监听的回调函数。 |
1224
1225**错误码**:
1226
1227以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
1228
1229| 错误码ID | 错误信息                                                     |
1230| -------- | ------------------------------------------------------------ |
1231| 201      | Permission denied.                                           |
1232| 202      | Permission denied, non-system app called system api.         |
1233| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
1234
1235**示例:**
1236
1237```js
1238// 取消监听单个回调函数
1239import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit';
1240
1241try {
1242  let callback = (event: KeyEvent) => {
1243    console.log(`Monitor on success ${JSON.stringify(event)}`);
1244  };
1245  let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP];
1246  inputMonitor.on('keyPressed', keys, callback);
1247  inputMonitor.off("keyPressed", callback);
1248} catch (error) {
1249  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
1250}
1251```
1252
1253```js
1254// 取消监听所有回调函数
1255import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit';
1256
1257try {
1258  let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP];
1259  inputMonitor.on('keyPressed', keys, (event: KeyEvent) => {
1260    console.log(`Monitor on success ${JSON.stringify(event)}`);
1261  });
1262  inputMonitor.off("keyPressed");
1263} catch (error) {
1264  console.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
1265}
1266```
1267