1# @ohos.multimodalInput.inputMonitor (输入监听)(系统接口) 2 3<!--Kit: Input Kit--> 4<!--Subsystem: MultimodalInput--> 5<!--Owner: @zhaoxueyuan--> 6<!--Designer: @hanruofei--> 7<!--Tester: @Lyuxin--> 8<!--Adviser: @Brilliantry_Rui--> 9 10输入监听模块,提供了监听输入设备事件的能力。输入设备事件当前包括触屏事件、鼠标输入事件和触控板输入事件。 11 12>**说明:** 13> 14>- 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> 16>- 文档中“全局”表示整个触控屏或触控板。如监听全局触屏事件,表示触摸触控板任何位置时,整个触控板的触屏事件均被监听。 17> 18>- 本模块接口均为系统接口。 19 20## 导入模块 21 22```js 23import { inputMonitor } from '@kit.InputKit'; 24``` 25 26## inputMonitor.on('touch') 27 28on(type: 'touch', receiver: TouchEventReceiver): void 29 30监听全局触屏事件。 31 32**需要权限:** ohos.permission.INPUT_MONITORING 33 34**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 35 36**参数:** 37 38| 参数名 | 类型 | 必填 | 说明 | 39| -------- | ---------------------------------------- | ---- | ------------------- | 40| type | string | 是 | 输入设备事件类型,取值'touch'。 | 41| receiver | [TouchEventReceiver](#toucheventreceiver) | 是 | 回调函数,异步上报触摸屏输入事件。 | 42 43**错误码**: 44 45以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 46 47| 错误码ID | 错误信息 | 48| ---- | --------------------- | 49| 201 | Permission denied. | 50| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 51 52**示例:** 53 54```js 55import { inputMonitor } from '@kit.InputKit'; 56import { TouchEvent } from '@kit.InputKit'; 57 58@Entry 59@Component 60struct Index { 61 build() { 62 RelativeContainer() { 63 Text() 64 .onClick(() => { 65 try { 66 inputMonitor.on('touch', (touchEvent: TouchEvent) => { 67 console.log(`Monitor on success ${JSON.stringify(touchEvent)}`); 68 return false; 69 }); 70 } catch (error) { 71 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 72 } 73 }) 74 } 75 } 76} 77``` 78 79## inputMonitor.on('mouse')<sup>9+</sup> 80 81on(type: 'mouse', receiver: Callback<MouseEvent>): void 82 83监听全局鼠标事件。 84 85**需要权限:** ohos.permission.INPUT_MONITORING 86 87**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 88 89**参数:** 90 91| 参数名 | 类型 | 必填 | 说明 | 92| -------- | -------------------------- | ---- | ------------------- | 93| type | string | 是 | 输入设备事件类型,取值'mouse'。 | 94| receiver | Callback<[MouseEvent](js-apis-mouseevent.md#mouseevent)> | 是 | 回调函数,异步上报鼠标输入事件。 | 95 96**错误码**: 97 98以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 99 100| 错误码ID | 错误信息 | 101| ---- | --------------------- | 102| 201 | Permission denied. | 103| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 104 105**示例:** 106 107```js 108import { inputMonitor } from '@kit.InputKit'; 109import { MouseEvent } from '@kit.InputKit'; 110 111@Entry 112@Component 113struct Index { 114 build() { 115 RelativeContainer() { 116 Text() 117 .onClick(() => { 118 try { 119 inputMonitor.on('mouse', (mouseEvent: MouseEvent) => { 120 console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`); 121 return false; 122 }); 123 } catch (error) { 124 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 125 } 126 }) 127 } 128 } 129} 130``` 131 132## inputMonitor.on('mouse')<sup>11+</sup> 133 134on(type: 'mouse', rect: display.Rect[], receiver: Callback<MouseEvent>): void 135 136监听鼠标事件,当鼠标移动至指定矩形区域内时,触发回调任务。 137 138**需要权限:** ohos.permission.INPUT_MONITORING 139 140**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 141 142**参数:** 143 144| 参数名 | 类型 | 必填 | 说明 | 145| -------- | -------------------------- | ---- | ------------------- | 146| type | string | 是 | 输入设备事件类型,取值'mouse'。 | 147| rect | display.Rect[] | 是 | 可以触发回调任务的矩形区域,可传入1至2个。 | 148| receiver | Callback<[MouseEvent](js-apis-mouseevent.md#mouseevent)> | 是 | 回调函数,异步上报鼠标输入事件。 | 149 150**错误码**: 151 152以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 153 154| 错误码ID | 错误信息 | 155| ---- | --------------------- | 156| 201 | Permission denied. | 157| 202 | SystemAPI permission error. | 158| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 159 160**示例:** 161 162```js 163import { inputMonitor } from '@kit.InputKit'; 164import { MouseEvent } from '@kit.InputKit'; 165import { display } from '@kit.ArkUI'; 166 167@Entry 168@Component 169struct Index { 170 build() { 171 RelativeContainer() { 172 Text() 173 .onClick(() => { 174 /** 175 * 鼠标在矩形区域内时,触发的回调任务。 176 */ 177 let callback = (mouseEvent : MouseEvent) => { 178 this.getUIContext().getPromptAction().showToast({ 179 message: `监听成功:${JSON.stringify(mouseEvent)}` 180 }) 181 console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`); 182 return false; 183 }; 184 185 /** 186 * 触发回调事件矩形区域。 187 */ 188 let rect: display.Rect[] = [{ 189 left: 100, 190 top: 100, 191 width: 100, 192 height: 100 193 }, { 194 left: 600, 195 top: 100, 196 width: 100, 197 height: 100 198 }]; 199 200 try { 201 inputMonitor.on('mouse', rect, callback); 202 } catch (error) { 203 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 204 } 205 }) 206 } 207 } 208} 209``` 210 211## inputMonitor.off('touch') 212 213off(type: 'touch', receiver?: TouchEventReceiver): void 214 215取消监听全局触屏事件。 216 217**需要权限:** ohos.permission.INPUT_MONITORING 218 219**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 220 221**参数:** 222 223| 参数名 | 类型 | 必填 | 说明 | 224| -------- | ---------------------------------------- | ---- | ------------------- | 225| type | string | 是 | 输入设备事件类型,取值'touch'。 | 226| receiver | [TouchEventReceiver](#toucheventreceiver) | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 227 228**错误码**: 229 230以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 231 232| 错误码ID | 错误信息 | 233| ---- | --------------------- | 234| 201 | Permission denied. | 235| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 236 237**示例:** 238 239```js 240import { inputMonitor } from '@kit.InputKit'; 241import { TouchEvent } from '@kit.InputKit'; 242 243@Entry 244@Component 245struct Index { 246 build() { 247 RelativeContainer() { 248 Text() 249 .onClick(() => { 250 // 取消监听单个回调函数 251 let callback = (touchEvent: TouchEvent) => { 252 console.log(`Monitor on success ${JSON.stringify(touchEvent)}`); 253 return false; 254 }; 255 try { 256 inputMonitor.on('touch', callback); 257 inputMonitor.off('touch', callback); 258 console.log(`Monitor off success`); 259 } catch (error) { 260 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 261 } 262 }) 263 } 264 } 265} 266``` 267 268```js 269import { inputMonitor } from '@kit.InputKit'; 270import { TouchEvent } from '@kit.InputKit'; 271 272@Entry 273@Component 274struct Index { 275 build() { 276 RelativeContainer() { 277 Text() 278 .onClick(() => { 279 // 取消监听所有回调函数 280 let callback = (touchEvent: TouchEvent) => { 281 console.log(`Monitor on success ${JSON.stringify(touchEvent)}`); 282 return false; 283 }; 284 try { 285 inputMonitor.on('touch', callback); 286 inputMonitor.off('touch'); 287 console.log(`Monitor off success`); 288 } catch (error) { 289 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 290 } 291 }) 292 } 293 } 294} 295``` 296 297## inputMonitor.off('mouse')<sup>9+</sup> 298 299off(type: 'mouse', receiver?: Callback<MouseEvent>): void 300 301取消监听全局鼠标事件。 302 303**需要权限:** ohos.permission.INPUT_MONITORING 304 305**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 306 307**参数:** 308 309| 参数名 | 类型 | 必填 | 说明 | 310| -------- | -------------------------- | ---- | ------------------- | 311| type | string | 是 | 输入设备事件类型,取值'mouse'。 | 312| receiver | Callback<MouseEvent> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 313 314**错误码**: 315 316以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 317 318| 错误码ID | 错误信息 | 319| ---- | --------------------- | 320| 201 | Permission denied. | 321| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 322 323**示例:** 324 325```js 326import { inputMonitor } from '@kit.InputKit'; 327import { MouseEvent } from '@kit.InputKit'; 328 329@Entry 330@Component 331struct Index { 332 build() { 333 RelativeContainer() { 334 Text() 335 .onClick(() => { 336 // 取消监听单个回调函数 337 let callback = (mouseEvent: MouseEvent) => { 338 console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`); 339 return false; 340 }; 341 try { 342 inputMonitor.on('mouse', callback); 343 inputMonitor.off('mouse', callback); 344 console.log(`Monitor off success`); 345 } catch (error) { 346 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 347 } 348 }) 349 } 350 } 351} 352``` 353 354```js 355import { inputMonitor } from '@kit.InputKit'; 356import { MouseEvent } from '@kit.InputKit'; 357 358@Entry 359@Component 360struct Index { 361 build() { 362 RelativeContainer() { 363 Text() 364 .onClick(() => { 365 // 取消监听所有回调函数 366 let callback = (mouseEvent: MouseEvent) => { 367 console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`); 368 return false; 369 }; 370 try { 371 inputMonitor.on('mouse', callback); 372 inputMonitor.off('mouse'); 373 console.log(`Monitor off success`); 374 } catch (error) { 375 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 376 } 377 }) 378 } 379 } 380} 381``` 382 383## TouchEventReceiver 384 385(touchEvent: TouchEvent): Boolean 386 387触屏输入事件的回调函数。 388 389**需要权限:** ohos.permission.INPUT_MONITORING 390 391**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 392 393**参数:** 394 395| 参数名 | 类型 | 必填 | 说明 | 396| ---------- | ---------------------------------------- | ---- | ---------------------------------------- | 397| touchEvent | [TouchEvent](js-apis-touchevent.md#touchevent) | 是 | 触屏输入事件。 | 398 399**返回值:** 400 401| 类型 | 说明 | 402| ------- | ---------------------------------------- | 403| Boolean | 若返回true,本次触屏后续产生的事件不再分发到窗口;若返回false,本次触屏后续产生的事件还会分发到窗口。 | 404 405**示例:** 406 407```js 408import { inputMonitor } from '@kit.InputKit'; 409import { TouchEvent } from '@kit.InputKit'; 410 411@Entry 412@Component 413struct Index { 414 build() { 415 RelativeContainer() { 416 Text() 417 .onClick(() => { 418 try { 419 inputMonitor.on('touch', touchEvent => { 420 if (touchEvent.touches.length == 3) { // 当前有三个手指按下 421 return true; 422 } 423 return false; 424 }); 425 } catch (error) { 426 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 427 } 428 }) 429 } 430 } 431} 432``` 433 434## inputMonitor.on('pinch')<sup>10+</sup> 435 436on(type: 'pinch', receiver: Callback<[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)>): void 437 438监听全局触控板的捏合事件。 439 440**需要权限:** ohos.permission.INPUT_MONITORING 441 442**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 443 444**参数:** 445 446| 参数名 | 类型 | 必填 | 说明 | 447| -------- | -------------------------- | ---- | ------------------- | 448| type | string | 是 | 输入设备事件类型,取值'pinch'。 | 449| receiver | Callback<[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)> | 是 | 回调函数,异步上报捏合输入事件。 | 450 451**错误码**: 452 453以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 454 455| 错误码ID | 错误信息 | 456| ---- | --------------------- | 457| 201 | Permission denied. | 458| 202 | SystemAPI permission error. | 459| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 460 461**示例:** 462 463```js 464import { inputMonitor } from '@kit.InputKit'; 465import { Pinch } from '@kit.InputKit'; 466 467@Entry 468@Component 469struct Index { 470 build() { 471 RelativeContainer() { 472 Text() 473 .onClick(() => { 474 try { 475 inputMonitor.on('pinch', (pinchEvent) => { 476 console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`); 477 return false; 478 }); 479 } catch (error) { 480 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 481 } 482 }) 483 } 484 } 485} 486``` 487 488## inputMonitor.off('pinch')<sup>10+</sup> 489 490off(type: 'pinch', receiver?: Callback<[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)>): void 491 492取消监听全局触控板的捏合事件。 493 494**需要权限:** ohos.permission.INPUT_MONITORING 495 496**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 497 498**参数:** 499 500| 参数名 | 类型 | 必填 | 说明 | 501| -------- | -------------------------- | ---- | ------------------- | 502| type | string | 是 | 输入设备事件类型,取值'pinch'。 | 503| receiver | Callback<[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 504 505**错误码**: 506 507以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 508 509| 错误码ID | 错误信息 | 510| ---- | --------------------- | 511| 201 | Permission denied. | 512| 202 | SystemAPI permission error. | 513| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 514 515**示例:** 516 517```js 518import { inputMonitor } from '@kit.InputKit'; 519import { Pinch } from '@kit.InputKit'; 520 521@Entry 522@Component 523struct Index { 524 build() { 525 RelativeContainer() { 526 Text() 527 .onClick(() => { 528 // 取消监听单个回调函数 529 let callback = (pinchEvent: Pinch) => { 530 console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`); 531 return false; 532 }; 533 try { 534 inputMonitor.on('pinch', callback); 535 inputMonitor.off('pinch', callback); 536 console.log(`Monitor off success`); 537 } catch (error) { 538 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 539 } 540 }) 541 } 542 } 543} 544``` 545 546```js 547import { inputMonitor } from '@kit.InputKit'; 548import { Pinch } from '@kit.InputKit'; 549 550@Entry 551@Component 552struct Index { 553 build() { 554 RelativeContainer() { 555 Text() 556 .onClick(() => { 557 // 取消监听所有回调函数 558 let callback = (pinchEvent: Pinch) => { 559 console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`); 560 return false; 561 }; 562 try { 563 inputMonitor.on('pinch', callback); 564 inputMonitor.off('pinch'); 565 console.log(`Monitor off success`); 566 } catch (error) { 567 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 568 } 569 }) 570 } 571 } 572} 573``` 574 575## inputMonitor.on('threeFingersSwipe')<sup>10+</sup> 576 577on(type: 'threeFingersSwipe', receiver: Callback<[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)>): void 578 579监听全局触控板的三指滑动事件。 580 581**需要权限:** ohos.permission.INPUT_MONITORING 582 583**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 584 585**参数:** 586 587| 参数名 | 类型 | 必填 | 说明 | 588| -------- | -------------------------- | ---- | ------------------- | 589| type | string | 是 | 输入设备事件类型,取值'threeFingersSwipe'。 | 590| receiver | Callback<[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)> | 是 | 回调函数,异步上报三指滑动输入事件。 | 591 592**错误码**: 593 594以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 595 596| 错误码ID | 错误信息 | 597| ---- | --------------------- | 598| 201 | Permission denied. | 599| 202 | SystemAPI permission error. | 600| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 601 602**示例:** 603 604```js 605import { inputMonitor } from '@kit.InputKit'; 606 607@Entry 608@Component 609struct Index { 610 build() { 611 RelativeContainer() { 612 Text() 613 .onClick(() => { 614 try { 615 inputMonitor.on('threeFingersSwipe', (threeFingersSwipe) => { 616 console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`); 617 return false; 618 }); 619 } catch (error) { 620 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 621 } 622 }) 623 } 624 } 625} 626``` 627 628## inputMonitor.off('threeFingersSwipe')<sup>10+</sup> 629 630off(type: 'threeFingersSwipe', receiver?: Callback<[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)>): void 631 632取消监听全局触控板的三指滑动事件。 633 634**需要权限:** ohos.permission.INPUT_MONITORING 635 636**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 637 638**参数:** 639 640| 参数名 | 类型 | 必填 | 说明 | 641| -------- | -------------------------- | ---- | ------------------- | 642| type | string | 是 | 输入设备事件类型,取值'threeFingersSwipe'。 | 643| receiver | Callback<[ThreeFingersSwipe](js-apis-multimodalinput-gestureevent.md#threefingersswipe)> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 644 645**错误码**: 646 647以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 648 649| 错误码ID | 错误信息 | 650| ---- | --------------------- | 651| 201 | Permission denied. | 652| 202 | SystemAPI permission error. | 653| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 654 655**示例:** 656 657```js 658import { inputMonitor } from '@kit.InputKit'; 659import { ThreeFingersSwipe } from '@kit.InputKit'; 660 661@Entry 662@Component 663struct Index { 664 build() { 665 RelativeContainer() { 666 Text() 667 .onClick(() => { 668 // 取消监听单个回调函数 669 let callback = (threeFingersSwipe: ThreeFingersSwipe) => { 670 console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`); 671 return false; 672 }; 673 try { 674 inputMonitor.on('threeFingersSwipe', callback); 675 inputMonitor.off("threeFingersSwipe", callback); 676 console.log(`Monitor off success`); 677 } catch (error) { 678 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 679 } 680 }) 681 } 682 } 683} 684``` 685 686```js 687import { inputMonitor } from '@kit.InputKit'; 688import { ThreeFingersSwipe } from '@kit.InputKit'; 689 690@Entry 691@Component 692struct Index { 693 build() { 694 RelativeContainer() { 695 Text() 696 .onClick(() => { 697 // 取消监听所有回调函数 698 let callback = (threeFingersSwipe: ThreeFingersSwipe) => { 699 console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`); 700 return false; 701 }; 702 try { 703 inputMonitor.on("threeFingersSwipe", callback); 704 inputMonitor.off("threeFingersSwipe"); 705 console.log(`Monitor off success`); 706 } catch (error) { 707 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 708 } 709 }) 710 } 711 } 712} 713``` 714 715## inputMonitor.on('fourFingersSwipe')<sup>10+</sup> 716 717on(type: 'fourFingersSwipe', receiver: Callback<[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)>): void 718 719监听全局触控板的四指滑动事件。 720 721**需要权限:** ohos.permission.INPUT_MONITORING 722 723**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 724 725**参数:** 726 727| 参数名 | 类型 | 必填 | 说明 | 728| -------- | -------------------------- | ---- | ------------------- | 729| type | string | 是 | 输入设备事件类型,取值'fourFingersSwipe'。 | 730| receiver | Callback<[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)> | 是 | 回调函数,异步上报四指滑动输入事件。 | 731 732**错误码**: 733 734以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 735 736| 错误码ID | 错误信息 | 737| ---- | --------------------- | 738| 201 | Permission denied. | 739| 202 | SystemAPI permission error. | 740| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 741 742**示例:** 743 744```js 745import { inputMonitor } from '@kit.InputKit'; 746 747@Entry 748@Component 749struct Index { 750 build() { 751 RelativeContainer() { 752 Text() 753 .onClick(() => { 754 try { 755 inputMonitor.on('fourFingersSwipe', (fourFingersSwipe) => { 756 console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`); 757 return false; 758 }); 759 } catch (error) { 760 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 761 } 762 }) 763 } 764 } 765} 766``` 767 768## inputMonitor.off('fourFingersSwipe')<sup>10+</sup> 769 770off(type: 'fourFingersSwipe', receiver?: Callback<[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)>): void 771 772取消监听全局触控板的四指滑动事件。 773 774**需要权限:** ohos.permission.INPUT_MONITORING 775 776**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 777 778**参数:** 779 780| 参数名 | 类型 | 必填 | 说明 | 781| -------- | -------------------------- | ---- | ------------------- | 782| type | string | 是 | 输入设备事件类型,取值'fourFingersSwipe'。 | 783| receiver | Callback<[FourFingersSwipe](js-apis-multimodalinput-gestureevent.md#fourfingersswipe)> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 784 785**错误码**: 786 787以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 788 789| 错误码ID | 错误信息 | 790| ---- | --------------------- | 791| 201 | Permission denied. | 792| 202 | SystemAPI permission error. | 793| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 794 795**示例:** 796 797```js 798import { inputMonitor } from '@kit.InputKit'; 799import { FourFingersSwipe } from '@kit.InputKit'; 800 801@Entry 802@Component 803struct Index { 804 build() { 805 RelativeContainer() { 806 Text() 807 .onClick(() => { 808 // 取消监听单个回调函数 809 let callback = (fourFingersSwipe: FourFingersSwipe) => { 810 console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`); 811 return false; 812 }; 813 try { 814 inputMonitor.on('fourFingersSwipe', callback); 815 inputMonitor.off('fourFingersSwipe', callback); 816 console.log(`Monitor off success`); 817 } catch (error) { 818 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 819 } 820 }) 821 } 822 } 823} 824``` 825 826```js 827import { inputMonitor } from '@kit.InputKit'; 828import { FourFingersSwipe } from '@kit.InputKit'; 829 830@Entry 831@Component 832struct Index { 833 build() { 834 RelativeContainer() { 835 Text() 836 .onClick(() => { 837 // 取消监听所有回调函数 838 let callback = (fourFingersSwipe: FourFingersSwipe) => { 839 console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`); 840 return false; 841 }; 842 try { 843 inputMonitor.on('fourFingersSwipe', callback); 844 inputMonitor.off('fourFingersSwipe'); 845 console.log(`Monitor off success`); 846 } catch (error) { 847 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 848 } 849 }) 850 } 851 } 852} 853``` 854 855## inputMonitor.on('rotate')<sup>11+</sup> 856 857on(type: 'rotate', fingers: number, receiver: Callback<Rotate>): void 858 859监听全局触控板的旋转事件。 860 861**需要权限:** ohos.permission.INPUT_MONITORING 862 863**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 864 865**参数:** 866 867| 参数名 | 类型 | 必填 | 说明 | 868| -------- | -------------------------- | ---- | ------------------- | 869| type | string | 是 | 输入设备事件类型,取值'rotate'。 | 870| fingers | number | 是 | 旋转的手指数,目前支持监听手指数是2。 | 871| receiver | Callback<[Rotate](js-apis-multimodalinput-gestureevent.md#rotate11)> | 是 | 回调函数,异步上报旋转输入事件。 | 872 873**错误码**: 874 875以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 876 877| 错误码ID | 错误信息 | 878| ---- | --------------------- | 879| 201 | Permission denied. | 880| 202 | SystemAPI permission error. | 881| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 882 883**示例:** 884 885```js 886import { inputMonitor } from '@kit.InputKit'; 887import { Rotate } from '@kit.InputKit'; 888 889@Entry 890@Component 891struct Index { 892 build() { 893 RelativeContainer() { 894 Text() 895 .onClick(() => { 896 try { 897 inputMonitor.on('rotate', 2, (rotateEvent: Rotate) => { 898 console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`); 899 return false; 900 }); 901 } catch (error) { 902 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 903 } 904 }) 905 } 906 } 907} 908``` 909 910## inputMonitor.off('rotate')<sup>11+</sup> 911 912off(type: 'rotate', fingers: number, receiver?: Callback<Rotate>): void 913 914取消监听全局触控板的旋转事件。 915 916**需要权限:** ohos.permission.INPUT_MONITORING 917 918**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 919 920**参数:** 921 922| 参数名 | 类型 | 必填 | 说明 | 923| -------- | -------------------------- | ---- | ------------------- | 924| type | string | 是 | 输入设备事件类型,取值'rotate'。 | 925| fingers | number | 是 | 旋转的手指数,目前支持监听手指数是2。 | 926| receiver | Callback<[Rotate](js-apis-multimodalinput-gestureevent.md#rotate11)> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 927 928**错误码**: 929 930以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 931 932| 错误码ID | 错误信息 | 933| ---- | --------------------- | 934| 201 | Permission denied. | 935| 202 | SystemAPI permission error. | 936| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 937 938**示例:** 939 940```js 941import { inputMonitor } from '@kit.InputKit'; 942import { Rotate } from '@kit.InputKit'; 943 944@Entry 945@Component 946struct Index { 947 build() { 948 RelativeContainer() { 949 Text() 950 .onClick(() => { 951 // 取消监听单个回调函数 952 let callback = (rotateEvent: Rotate) => { 953 console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`); 954 return false; 955 }; 956 try { 957 inputMonitor.on('rotate', 2, callback); 958 inputMonitor.off('rotate', 2, callback); 959 console.log(`Monitor off success`); 960 } catch (error) { 961 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 962 } 963 }) 964 } 965 } 966} 967``` 968 969```js 970import { inputMonitor } from '@kit.InputKit'; 971import { Rotate } from '@kit.InputKit'; 972 973@Entry 974@Component 975struct Index { 976 build() { 977 RelativeContainer() { 978 Text() 979 .onClick(() => { 980 // 取消监听所有回调函数 981 let callback = (rotateEvent: Rotate) => { 982 console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`); 983 return false; 984 }; 985 try { 986 inputMonitor.on('rotate', 2, callback); 987 inputMonitor.off('rotate', 2); 988 console.log(`Monitor off success`); 989 } catch (error) { 990 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 991 } 992 }) 993 } 994 } 995} 996``` 997 998## inputMonitor.on('pinch')<sup>11+</sup> 999 1000on(type: 'pinch', fingers: number, receiver: Callback<Pinch>): void 1001 1002监听全局触控板的捏合事件。 1003 1004**需要权限:** ohos.permission.INPUT_MONITORING 1005 1006**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1007 1008**参数:** 1009 1010| 参数名 | 类型 | 必填 | 说明 | 1011| -------- | -------------------------- | ---- | ------------------- | 1012| type | string | 是 | 输入设备事件类型,取值'pinch'。 | 1013| fingers | number | 是 | 捏合的手指数,取值范围:大于等于2。 | 1014| receiver | Callback<[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)> | 是 | 回调函数,异步上报捏合输入事件。 | 1015 1016**错误码**: 1017 1018以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1019 1020| 错误码ID | 错误信息 | 1021| ---- | --------------------- | 1022| 201 | Permission denied. | 1023| 202 | SystemAPI permission error. | 1024| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1025 1026**示例:** 1027 1028```js 1029import { inputMonitor } from '@kit.InputKit'; 1030import { Pinch } from '@kit.InputKit'; 1031 1032@Entry 1033@Component 1034struct Index { 1035 build() { 1036 RelativeContainer() { 1037 Text() 1038 .onClick(() => { 1039 try { 1040 inputMonitor.on('pinch', 2, (pinchEvent: Pinch) => { 1041 console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`); 1042 return false; 1043 }); 1044 } catch (error) { 1045 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1046 } 1047 }) 1048 } 1049 } 1050} 1051``` 1052 1053## inputMonitor.off('pinch')<sup>11+</sup> 1054 1055off(type: 'pinch', fingers: number, receiver?: Callback<Pinch>): void 1056 1057取消监听全局触控板的捏合事件。 1058 1059**需要权限:** ohos.permission.INPUT_MONITORING 1060 1061**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1062 1063**参数:** 1064 1065| 参数名 | 类型 | 必填 | 说明 | 1066| -------- | -------------------------- | ---- | ------------------- | 1067| type | string | 是 | 输入设备事件类型,取值'pinch'。 | 1068| fingers | number | 是 | 捏合的手指数,取值范围:大于等于2。 | 1069| receiver | Callback<[Pinch](js-apis-multimodalinput-gestureevent.md#pinch)> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 1070 1071**错误码**: 1072 1073以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1074 1075| 错误码ID | 错误信息 | 1076| ---- | --------------------- | 1077| 201 | Permission denied. | 1078| 202 | SystemAPI permission error. | 1079| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1080 1081**示例:** 1082 1083```js 1084import { inputMonitor } from '@kit.InputKit'; 1085import { Pinch } from '@kit.InputKit'; 1086 1087@Entry 1088@Component 1089struct Index { 1090 build() { 1091 RelativeContainer() { 1092 Text() 1093 .onClick(() => { 1094 // 取消监听单个回调函数 1095 let callback = (pinchEvent: Pinch) => { 1096 console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`); 1097 return false; 1098 }; 1099 try { 1100 inputMonitor.on('pinch', 2, callback); 1101 inputMonitor.off('pinch', 2, callback); 1102 console.log(`Monitor off success`); 1103 } catch (error) { 1104 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1105 } 1106 }) 1107 } 1108 } 1109} 1110``` 1111 1112```js 1113import { inputMonitor } from '@kit.InputKit'; 1114import { Pinch } from '@kit.InputKit'; 1115 1116@Entry 1117@Component 1118struct Index { 1119 build() { 1120 RelativeContainer() { 1121 Text() 1122 .onClick(() => { 1123 // 取消监听所有回调函数 1124 let callback = (pinchEvent: Pinch) => { 1125 console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`); 1126 return false; 1127 }; 1128 try { 1129 inputMonitor.on('pinch', 2, callback); 1130 inputMonitor.off('pinch', 2); 1131 console.log(`Monitor off success`); 1132 } catch (error) { 1133 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1134 } 1135 }) 1136 } 1137 } 1138} 1139``` 1140 1141## inputMonitor.on('threeFingersTap')<sup>11+</sup> 1142 1143on(type: 'threeFingersTap', receiver: Callback<[ThreeFingersTap](js-apis-multimodalinput-gestureevent.md#threefingerstap11)>): void 1144 1145监听全局触控板的三指轻点事件。 1146 1147**需要权限:** ohos.permission.INPUT_MONITORING 1148 1149**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1150 1151**参数:** 1152 1153| 参数名 | 类型 | 必填 | 说明 | 1154| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------- | 1155| type | string | 是 | 输入设备事件类型,取值'threeFingersTap'。 | 1156| receiver | Callback<[ThreeFingersTap](js-apis-multimodalinput-gestureevent.md#threefingerstap11)> | 是 | 回调函数,异步上报三指轻点输入事件。 | 1157 1158**错误码**: 1159 1160以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1161 1162| 错误码ID | 错误信息 | 1163| ---- | --------------------- | 1164| 201 | Permission denied. | 1165| 202 | SystemAPI permission error. | 1166| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1167 1168**示例:** 1169 1170```js 1171import { inputMonitor } from '@kit.InputKit'; 1172 1173@Entry 1174@Component 1175struct Index { 1176 build() { 1177 RelativeContainer() { 1178 Text() 1179 .onClick(() => { 1180 try { 1181 inputMonitor.on('threeFingersTap', (threeFingersTap) => { 1182 console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`); 1183 return false; 1184 }); 1185 } catch (error) { 1186 console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1187 } 1188 }) 1189 } 1190 } 1191} 1192``` 1193 1194## inputMonitor.off('threeFingersTap')<sup>11+</sup> 1195 1196off(type: 'threeFingersTap', receiver?: Callback<[ThreeFingersTap](js-apis-multimodalinput-gestureevent.md#threefingerstap11)>): void 1197 1198取消监听全局触控板的三指轻点事件。 1199 1200**需要权限:** ohos.permission.INPUT_MONITORING 1201 1202**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1203 1204**参数:** 1205 1206| 参数名 | 类型 | 必填 | 说明 | 1207| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1208| type | string | 是 | 输入设备事件类型,取值'threeFingersTap'。 | 1209| receiver | Callback<[ThreeFingersTap](js-apis-multimodalinput-gestureevent.md#threefingerstap11)> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 1210 1211**错误码**: 1212 1213以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1214 1215| 错误码ID | 错误信息 | 1216| ---- | --------------------- | 1217| 201 | Permission denied. | 1218| 202 | SystemAPI permission error. | 1219| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1220 1221**示例:** 1222 1223```js 1224import { inputMonitor } from '@kit.InputKit'; 1225import { ThreeFingersTap } from '@kit.InputKit'; 1226 1227@Entry 1228@Component 1229struct Index { 1230 build() { 1231 RelativeContainer() { 1232 Text() 1233 .onClick(() => { 1234 // 取消监听单个回调函数 1235 let callback = (threeFingersTap: ThreeFingersTap) => { 1236 console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`); 1237 return false; 1238 }; 1239 try { 1240 inputMonitor.on('threeFingersTap', callback); 1241 inputMonitor.off("threeFingersTap", callback); 1242 console.log(`Monitor off success`); 1243 } catch (error) { 1244 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1245 } 1246 }) 1247 } 1248 } 1249} 1250``` 1251 1252```js 1253import { inputMonitor } from '@kit.InputKit'; 1254import { ThreeFingersTap } from '@kit.InputKit'; 1255 1256@Entry 1257@Component 1258struct Index { 1259 build() { 1260 RelativeContainer() { 1261 Text() 1262 .onClick(() => { 1263 // 取消监听所有回调函数 1264 let callback = (threeFingersTap: ThreeFingersTap) => { 1265 console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`); 1266 return false; 1267 }; 1268 try { 1269 inputMonitor.on('threeFingersTap', callback); 1270 inputMonitor.off("threeFingersTap"); 1271 console.log(`Monitor off success`); 1272 } catch (error) { 1273 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1274 } 1275 }) 1276 } 1277 } 1278} 1279``` 1280 1281## inputMonitor.on('touchscreenSwipe')<sup>18+</sup> 1282 1283on(type: 'touchscreenSwipe', fingers: number, receiver: Callback<TouchGestureEvent>): void 1284 1285监听触摸屏滑动手势事件。 1286 1287**需要权限:** ohos.permission.INPUT_MONITORING 1288 1289**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1290 1291**参数:** 1292 1293| 参数名 | 类型 | 必填 | 说明 | 1294| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1295| type | string | 是 | 输入设备事件类型,取值'touchscreenSwipe'。 | 1296| fingers | number | 是 | 滑动手势的手指数,取值范围:[3,5]。 | 1297| receiver | Callback<[TouchGestureEvent](js-apis-multimodalinput-gestureevent-sys.md#touchgestureevent)> | 是 | 回调函数,异步上报触摸屏滑动手势事件。 | 1298 1299**错误码**: 1300 1301以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1302 1303| 错误码ID | 错误信息 | 1304| ---- | --------------------- | 1305| 201 | Permission denied. | 1306| 202 | SystemAPI permission error. | 1307| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1308 1309**示例:** 1310 1311```js 1312import { inputMonitor } from '@kit.InputKit'; 1313import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent'; 1314 1315@Entry 1316@Component 1317struct Index { 1318 build() { 1319 RelativeContainer() { 1320 Text() 1321 .onClick(() => { 1322 let fingers: number = 4; 1323 try { 1324 inputMonitor.on('touchscreenSwipe', fingers, (event: TouchGestureEvent) => { 1325 console.log(`Monitor on success ${JSON.stringify(event)}`); 1326 }); 1327 } catch (error) { 1328 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1329 } 1330 }) 1331 } 1332 } 1333} 1334``` 1335 1336## inputMonitor.off('touchscreenSwipe')<sup>18+</sup> 1337 1338off(type: 'touchscreenSwipe', fingers: number, receiver?: Callback<TouchGestureEvent>): void 1339 1340取消监听触摸屏滑动手势事件。 1341 1342**需要权限:** ohos.permission.INPUT_MONITORING 1343 1344**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1345 1346**参数:** 1347 1348| 参数名 | 类型 | 必填 | 说明 | 1349| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1350| type | string | 是 | 输入设备事件类型,取值'touchscreenSwipe'。 | 1351| fingers | number | 是 | 滑动手势的手指数,取值范围:[3,5]。 | 1352| receiver | Callback<[TouchGestureEvent](js-apis-multimodalinput-gestureevent-sys.md#touchgestureevent)> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 1353 1354**错误码**: 1355 1356以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1357 1358| 错误码ID | 错误信息 | 1359| ---- | --------------------- | 1360| 201 | Permission denied. | 1361| 202 | SystemAPI permission error. | 1362| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1363 1364**示例:** 1365 1366```js 1367import { inputMonitor } from '@kit.InputKit'; 1368import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent'; 1369 1370@Entry 1371@Component 1372struct Index { 1373 build() { 1374 RelativeContainer() { 1375 Text() 1376 .onClick(() => { 1377 // 取消监听单个回调函数 1378 let callback = (event: TouchGestureEvent) => { 1379 console.log(`Monitor on success ${JSON.stringify(event)}`); 1380 }; 1381 let fingers: number = 4; 1382 try { 1383 inputMonitor.on('touchscreenSwipe', fingers, callback); 1384 inputMonitor.off('touchscreenSwipe', fingers, callback); 1385 } catch (error) { 1386 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1387 } 1388 }) 1389 } 1390 } 1391} 1392``` 1393 1394```js 1395import { inputMonitor } from '@kit.InputKit'; 1396import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent'; 1397 1398@Entry 1399@Component 1400struct Index { 1401 build() { 1402 RelativeContainer() { 1403 Text() 1404 .onClick(() => { 1405 // 取消监听所有回调函数 1406 let fingers: number = 4; 1407 try { 1408 inputMonitor.on('touchscreenSwipe', fingers, (event: TouchGestureEvent) => { 1409 console.log(`Monitor on success ${JSON.stringify(event)}`); 1410 }); 1411 inputMonitor.off('touchscreenSwipe', fingers); 1412 } catch (error) { 1413 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1414 } 1415 }) 1416 } 1417 } 1418} 1419``` 1420 1421## inputMonitor.on('touchscreenPinch')<sup>18+</sup> 1422 1423on(type: 'touchscreenPinch', fingers: number, receiver: Callback<TouchGestureEvent>): void 1424 1425监听触摸屏捏合手势事件。 1426 1427**需要权限:** ohos.permission.INPUT_MONITORING 1428 1429**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1430 1431**参数:** 1432 1433| 参数名 | 类型 | 必填 | 说明 | 1434| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1435| type | string | 是 | 输入设备事件类型,取值'touchscreenPinch'。 | 1436| fingers | number | 是 | 捏合手势的手指数,取值范围:[4,5]。 | 1437| receiver | Callback<[TouchGestureEvent](js-apis-multimodalinput-gestureevent-sys.md#touchgestureevent)> | 是 | 回调函数,异步上报触摸屏捏合手势事件。 | 1438 1439**错误码**: 1440 1441以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1442 1443| 错误码ID | 错误信息 | 1444| ---- | --------------------- | 1445| 201 | Permission denied. | 1446| 202 | SystemAPI permission error. | 1447| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1448 1449**示例:** 1450 1451```js 1452import { inputMonitor } from '@kit.InputKit'; 1453import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent'; 1454 1455@Entry 1456@Component 1457struct Index { 1458 build() { 1459 RelativeContainer() { 1460 Text() 1461 .onClick(() => { 1462 let fingers: number = 4; 1463 try { 1464 inputMonitor.on('touchscreenPinch', fingers, (event: TouchGestureEvent) => { 1465 console.log(`Monitor on success ${JSON.stringify(event)}`); 1466 }); 1467 } catch (error) { 1468 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1469 } 1470 }) 1471 } 1472 } 1473} 1474``` 1475 1476## inputMonitor.off('touchscreenPinch')<sup>18+</sup> 1477 1478off(type: 'touchscreenPinch', fingers: number, receiver?: Callback<TouchGestureEvent>): void 1479 1480取消监听触摸屏捏合手势事件。 1481 1482**需要权限:** ohos.permission.INPUT_MONITORING 1483 1484**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1485 1486**参数:** 1487 1488| 参数名 | 类型 | 必填 | 说明 | 1489| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1490| type | string | 是 | 输入设备事件类型,取值'touchscreenPinch'。 | 1491| fingers | number | 是 | 捏合手势的手指数,取值范围:[4,5]。 | 1492| receiver | Callback<[TouchGestureEvent](js-apis-multimodalinput-gestureevent-sys.md#touchgestureevent)> | 否 | 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。 | 1493 1494**错误码**: 1495 1496以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1497 1498| 错误码ID | 错误信息 | 1499| ---- | --------------------- | 1500| 201 | Permission denied. | 1501| 202 | SystemAPI permission error. | 1502| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1503 1504**示例:** 1505 1506```js 1507import { inputMonitor } from '@kit.InputKit'; 1508import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent'; 1509 1510@Entry 1511@Component 1512struct Index { 1513 build() { 1514 RelativeContainer() { 1515 Text() 1516 .onClick(() => { 1517 // 取消监听单个回调函数 1518 let callback = (event: TouchGestureEvent) => { 1519 console.log(`Monitor on success ${JSON.stringify(event)}`); 1520 }; 1521 let fingers: number = 4; 1522 try { 1523 inputMonitor.on('touchscreenPinch', fingers, callback); 1524 inputMonitor.off("touchscreenPinch", fingers, callback); 1525 } catch (error) { 1526 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1527 } 1528 }) 1529 } 1530 } 1531} 1532``` 1533 1534```js 1535import { inputMonitor } from '@kit.InputKit'; 1536import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent'; 1537 1538@Entry 1539@Component 1540struct Index { 1541 build() { 1542 RelativeContainer() { 1543 Text() 1544 .onClick(() => { 1545 // 取消监听所有回调函数 1546 let fingers: number = 4; 1547 try { 1548 inputMonitor.on('touchscreenPinch', fingers, (event: TouchGestureEvent) => { 1549 console.log(`Monitor on success ${JSON.stringify(event)}`); 1550 }); 1551 inputMonitor.off("touchscreenPinch", fingers); 1552 } catch (error) { 1553 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1554 } 1555 }) 1556 } 1557 } 1558} 1559``` 1560 1561## inputMonitor.on('keyPressed')<sup>15+</sup> 1562 1563on(type: 'keyPressed', keys: Array<KeyCode>, receiver: Callback<KeyEvent>): void 1564 1565监听指定按键的按下抬起事件,支持监听META_LEFT键、META_RIGHT键、电源键、音量键。 1566 1567**需要权限:** ohos.permission.INPUT_MONITORING 1568 1569**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1570 1571**参数:** 1572 1573| 参数名 | 类型 | 必填 | 说明 | 1574| -------- | ----------------------------------------------------------- | ---- | ------------------------------------ | 1575| type | string | 是 | 按键事件类型,取唯一值'keyPressed'。 | 1576| keys | Array<[KeyCode](js-apis-keycode.md#keycode)> | 是 | 按键码列表,支持如下取值:KEYCODE_META_LEFT、KEYCODE_META_RIGHT、KEYCODE_POWER、KEYCODE_VOLUME_DOWN、KEYCODE_VOLUME_UP。 | 1577| receiver | Callback<[KeyEvent](js-apis-keyevent.md#keyevent)> | 是 | 用于接收上报数据的回调函数。 | 1578 1579**错误码**: 1580 1581以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[按键监听错误码](./errorcode-inputmonitor.md)。 1582 1583| 错误码ID | 错误信息 | 1584| -------- | ------------------------------------------------------------ | 1585| 201 | Permission denied. | 1586| 202 | Permission denied, non-system app called system api. | 1587| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1588| 4100001 | Event listening not supported for the key. | 1589 1590**示例:** 1591 1592```js 1593import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit'; 1594 1595@Entry 1596@Component 1597struct Index { 1598 build() { 1599 RelativeContainer() { 1600 Text() 1601 .onClick(() => { 1602 try { 1603 let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP]; 1604 inputMonitor.on('keyPressed', keys, (event: KeyEvent ) => { 1605 console.log(`Monitor on success ${JSON.stringify(event)}`); 1606 }); 1607 } catch (error) { 1608 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1609 } 1610 }) 1611 } 1612 } 1613} 1614``` 1615 1616## inputMonitor.off('keyPressed')<sup>15+</sup> 1617 1618off(type: 'keyPressed', receiver?: Callback<KeyEvent>): void 1619 1620取消监听按键按下抬起事件。支持取消监听META_LEFT键、META_RIGHT键、电源键、音量键。需和inputMonitor.on('keyPressed')配套使用。 1621 1622**需要权限:** ohos.permission.INPUT_MONITORING 1623 1624**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1625 1626**参数:** 1627 1628| 参数名 | 类型 | 必填 | 说明 | 1629| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | 1630| type | string | 是 | 按键事件类型,取唯一值'keyPressed'。 | 1631| receiver | Callback<[KeyEvent](js-apis-keyevent.md#keyevent)> | 否 | 需要取消监听的回调函数。若不填,取消应用所有按键监听的回调函数。 | 1632 1633**错误码**: 1634 1635以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1636 1637| 错误码ID | 错误信息 | 1638| -------- | ------------------------------------------------------------ | 1639| 201 | Permission denied. | 1640| 202 | Permission denied, non-system app called system api. | 1641| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 1642 1643**示例:** 1644 1645```js 1646import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit'; 1647 1648@Entry 1649@Component 1650struct Index { 1651 build() { 1652 RelativeContainer() { 1653 Text() 1654 .onClick(() => { 1655 // 取消监听单个回调函数 1656 try { 1657 let callback = (event: KeyEvent) => { 1658 console.log(`Monitor on success ${JSON.stringify(event)}`); 1659 }; 1660 let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP]; 1661 inputMonitor.on('keyPressed', keys, callback); 1662 inputMonitor.off("keyPressed", callback); 1663 } catch (error) { 1664 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1665 } 1666 }) 1667 } 1668 } 1669} 1670``` 1671 1672```js 1673import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit'; 1674 1675@Entry 1676@Component 1677struct Index { 1678 build() { 1679 RelativeContainer() { 1680 Text() 1681 .onClick(() => { 1682 // 取消监听所有回调函数 1683 try { 1684 let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP]; 1685 inputMonitor.on('keyPressed', keys, (event: KeyEvent) => { 1686 console.log(`Monitor on success ${JSON.stringify(event)}`); 1687 }); 1688 inputMonitor.off("keyPressed"); 1689 } catch (error) { 1690 console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 1691 } 1692 }) 1693 } 1694 } 1695} 1696``` 1697 1698## inputMonitor.queryTouchEvents()<sup>20+</sup> 1699 1700queryTouchEvents(count: number): Promise<Array<TouchEvent>> 1701 1702查询最近的触屏事件,最多支持查询 100 条事件,使用Promise异步回调。 1703 1704**需要权限:** ohos.permission.INPUT_MONITORING 1705 1706**系统能力:** SystemCapability.MultimodalInput.Input.InputMonitor 1707 1708**参数:** 1709 1710| 参数名 | 类型 | 必填 | 说明 | 1711| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | 1712| count | number | 是 | 需要查询的触屏事件数量,取值范围为0到100的整数。小于0时取值为0、大于100时取值为100。如果实际触屏事件只有30个,但该参数取值为50 ,则仅支持查询到30个触屏事件。 | 1713 1714**返回值:** 1715 1716| 类型 | 说明 | 1717| :------------ | :---------------------------------- | 1718| Promise<Array<[TouchEvent](js-apis-touchevent-sys.md#touchevent)>> | Promise对象,返回查询到的触屏事件。包含以下有效信息:<br/>- actionTime:触屏事件发生的时间,表示从1970.1.1 00:00:00 GMT逝去的微秒数。<br/>- [SourceType](js-apis-touchevent.md#sourcetype):触摸来源的设备类型。<br/>- [isInject](js-apis-touchevent-sys.md#touchevent):表示该触屏事件是否为注入事件。<br/>- pressure:压力值,取值范围是[0.0, 1.0],0.0表示不支持。<br/>- tiltX:相对YZ平面的角度,取值的范围[-90, 90],其中正值是向右倾斜。<br/>- tiltY:相对XZ平面的角度,取值的范围[-90, 90],其中正值是向下倾斜。 | 1719 1720**错误码**: 1721 1722以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1723 1724| 错误码ID | 错误信息 | 1725| -------- | ------------------------------------------------------------ | 1726| 201 | Permission denied. | 1727| 202 | Permission denied, non-system app called system api. | 1728 1729**示例:** 1730 1731```js 1732import { inputMonitor, TouchEvent } from '@kit.InputKit' 1733import { BusinessError } from '@kit.BasicServicesKit'; 1734 1735try { 1736 inputMonitor.queryTouchEvents(10).then((events: Array<TouchEvent>) => { 1737 events.forEach((event, index) => { 1738 console.info(`Touch event ${index}: actionTime=${event.actionTime}, sourceType=${event.sourceType}`); 1739 }); 1740 }).catch((error: BusinessError) => { 1741 console.error('queryTouchEvents promise error: ' + JSON.stringify(error)); 1742 }); 1743} catch (error) { 1744 const code = (error as BusinessError).code; 1745 const message = (error as BusinessError).message; 1746 console.error(`queryTouchEvents failed, error code: ${code}, message: ${message}.`); 1747} 1748``` 1749