1# Input Monitor Development 2 3## When to Use 4 5The **inputMonitor** module provides capabilities such as listening for key events and touchpad gestures. For example, if your application needs to implement a dedicated function when a three-finger swipe-up gesture occurs on the touchpad, you can listen for three-finger swipe-up events to serve that purpose. 6 7## Modules to Import 8 9```js 10import { inputMonitor } from '@kit.InputKit'; 11``` 12 13## Available APIs 14 15The following table lists the common APIs provided by the **inputMonitor** module. For details, see [ohos.multimodalInput.inputMonitor](../../reference/apis-input-kit/js-apis-inputmonitor-sys.md). 16 17| API | Description| 18| ------------------------------------------------------------ | -------------------------- | 19| on(type: 'mouse', receiver: Callback<MouseEvent>): void |Listens for mouse events.| 20| on(type: 'touch', receiver: TouchEventReceiver): void | Listens for touchscreen events.| 21| on(type: 'pinch', receiver: TouchEventReceiver): void | Listens for pinch events.| 22| on(type: 'threeFingersSwipe', receiver: Callback<ThreeFingersSwipe>): void | Listens for three-finger swipe-up events.| 23| on(type: 'threeFingersTap', receiver: Callback<ThreeFingersSwipe>): void | Listens for three-finger tap events.| 24| on(type: 'fourFingersSwipe', receiver: Callback<FourFingersSwipe>): void | Listens for four-finger swipe events.| 25| on(type: 'rotate', fingers: number, receiver: Callback<Rotate>): void | Listens for rotation events.| 26| off(type: 'mouse', receiver: Callback<MouseEvent>): void |Cancels listening for mouse events.| 27| off(type: 'touch', receiver: TouchEventReceiver): void | Cancels listening for touchscreen events.| 28| off(type: 'pinch', receiver: TouchEventReceiver): void | Cancels listening for pinch events.| 29| off(type: 'threeFingersSwipe', receiver: Callback<ThreeFingersSwipe>): void | Cancels listening for three-finger swipe-up events.| 30| off(type: 'threeFingersTap', receiver: Callback<ThreeFingersSwipe>): void | Cancels listening for three-finger tap events.| 31| off(type: 'fourFingersSwipe', receiver: Callback<FourFingersSwipe>): void | Cancels listening for four-finger swipe events.| 32| off(type: 'rotate', fingers: number, receiver: Callback<Rotate>): void | Cancels listening for rotation events.| 33 34## How to Develop 35 36This example assumes that the application needs to change the style based on the mouse button pressing status. Specifically, listen for mouse button events by calling [on](../../reference/apis-input-kit/js-apis-inputmonitor-sys.md#inputmonitoronmouse9), and cancel listening for mouse button events by calling [off](../../reference/apis-input-kit/js-apis-inputmonitor-sys.md#inputmonitoroffmouse9). 37 38```js 39import { MouseEvent } from '@kit.InputKit'; 40 41let BUTTON_DOWN = 2; 42let callback = (mouseEvent: MouseEvent) => { 43 console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`); 44 if(mouseEvent.action = BUTTON_DOWN){ 45 return true;// Callback triggered when the mouse button is pressed. 46 } 47 return false; 48}; 49 50try { 51 inputMonitor.on('mouse', (mouseEvent: MouseEvent) => {// Start to listen for mouse events. 52 console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`); 53 return false; 54 }); 55} catch (error) { 56 console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 57} 58// Callback triggered when the mouse button is pressed. 59try { 60 inputMonitor.off('mouse', callback);// Cancel listening for mouse events. 61 console.log(`Monitor off success`); 62} catch (error) { 63 console.log(`Monitor off failed, error: ${JSON.stringify(error, [`code`, `message`])}`); 64} 65``` 66