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