1# @ohos.multimodalInput.inputMonitor (Input Monitor) 2 3The **inputMonitor** module implements listening for events of input devices (namely, touchscreen and mouse). 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - The APIs provided by this module are system APIs. 9 10 11## Modules to Import 12 13 14```js 15import inputMonitor from '@ohos.multimodalInput.inputMonitor'; 16``` 17 18 19## inputMonitor.on 20 21on(type: "touch", receiver: TouchEventReceiver): void 22 23Enables listening for global touch events. 24 25**Required permissions**: ohos.permission.INPUT_MONITORING 26 27**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor 28 29**Parameters** 30 31| Name | Type | Mandatory | Description | 32| -------- | ---------------------------------------- | ---- | ------------------- | 33| type | string | Yes | Type of the input device event. The value is **touch**.| 34| receiver | [TouchEventReceiver](#toucheventreceiver) | Yes | Callback used to return the touch event.| 35 36**Example** 37 38```js 39try { 40 inputMonitor.on("touch", (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<sup>9+</sup> 50 51on(type: "mouse", receiver: Callback<MouseEvent>): void 52 53Enables listening for global mouse events. 54 55**Required permissions**: ohos.permission.INPUT_MONITORING 56 57**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor 58 59**Parameters** 60 61| Name | Type | Mandatory | Description | 62| -------- | -------------------------- | ---- | ------------------- | 63| type | string | Yes | Type of the input device event. The value is **mouse**.| 64| receiver | Callback<MouseEvent> | Yes | Callback used to return the mouse event. | 65 66 **Example** 67 68```js 69try { 70 inputMonitor.on("mouse", (mouseEvent) => { 71 console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`); 72 return false; 73 }); 74} catch (error) { 75 console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 76} 77``` 78 79 80 81## inputMonitor.off 82 83off(type: "touch", receiver?: TouchEventReceiver): void 84 85Disables listening for global touch events. 86 87**Required permissions**: ohos.permission.INPUT_MONITORING 88 89**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor 90 91**Parameters** 92 93| Name | Type | Mandatory | Description | 94| -------- | ---------------------------------------- | ---- | ------------------- | 95| type | string | Yes | Type of the input device event. The value is **touch**.| 96| receiver | [TouchEventReceiver](#toucheventreceiver) | No | Callback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application. | 97 98**Example** 99 100```js 101// Disable listening for a single callback function. 102function callback(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// Cancel listening for all callback functions. 117function callback(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<sup>9+</sup> 131 132off(type: "mouse", receiver?: Callback<MouseEvent>): void 133 134Stops listening for global mouse events. 135 136**Required permissions**: ohos.permission.INPUT_MONITORING 137 138**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor 139 140**Parameters** 141 142| Name | Type | Mandatory | Description | 143| -------- | -------------------------- | ---- | ------------------- | 144| type | string | Yes | Type of the input device event. The value is **mouse**.| 145| receiver | Callback<MouseEvent> | No | Callback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.| 146 147**Example** 148 149```js 150// Disable listening for a single callback. 151function callback(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// Disable listening for all callback functions. 166function callback(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 181Represents the callback used to return the touch event. 182 183**Required permissions**: ohos.permission.INPUT_MONITORING 184 185**System capability**: SystemCapability.MultimodalInput.Input.InputMonitor 186 187**Parameters** 188 189| Name | Type | Mandatory | Description | 190| ---------- | ---------------------------------------- | ---- | ---------------------------------------- | 191| touchEvent | [TouchEvent](../arkui-js/js-components-common-events.md) | Yes | Touch event.| 192 193**Return value** 194 195| Type | Description | 196| ------- | ---------------------------------------- | 197| Boolean | Result indicating whether the touch event will be dispatched to the window. The value **true** indicates that the touch event will be dispatched to the window, and the value **false** indicates the opposite.| 198 199**Example** 200 201```js 202try { 203 inputMonitor.on("touch", touchEvent => { 204 if (touchEvent.touches.length == 3) {// Three fingers are pressed. 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