harmony 鸿蒙Input Monitor Development

2025-06-06 浏览 (1)

Input Monitor Development

When to Use

The 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.

Modules to Import

import { inputMonitor } from '@kit.InputKit';

Available APIs

The following table lists the common APIs provided by the inputMonitor module. For details, see ohos.multimodalInput.inputMonitor.

APIDescription
on(type: 'mouse', receiver: Callback<MouseEvent>): voidListens for mouse events.
on(type: 'touch', receiver: TouchEventReceiver): voidListens for touchscreen events.
on(type: 'pinch', receiver: TouchEventReceiver): voidListens for pinch events.
on(type: 'threeFingersSwipe', receiver: Callback<ThreeFingersSwipe>): voidListens for three-finger swipe-up events.
on(type: 'threeFingersTap', receiver: Callback<ThreeFingersSwipe>): voidListens for three-finger tap events.
on(type: 'fourFingersSwipe', receiver: Callback<FourFingersSwipe>): voidListens for four-finger swipe events.
on(type: 'rotate', fingers: number, receiver: Callback<Rotate>): voidListens for rotation events.
off(type: 'mouse', receiver: Callback<MouseEvent>): voidCancels listening for mouse events.
off(type: 'touch', receiver: TouchEventReceiver): voidCancels listening for touchscreen events.
off(type: 'pinch', receiver: TouchEventReceiver): voidCancels listening for pinch events.
off(type: 'threeFingersSwipe', receiver: Callback<ThreeFingersSwipe>): voidCancels listening for three-finger swipe-up events.
off(type: 'threeFingersTap', receiver: Callback<ThreeFingersSwipe>): voidCancels listening for three-finger tap events.
off(type: 'fourFingersSwipe', receiver: Callback<FourFingersSwipe>): voidCancels listening for four-finger swipe events.
off(type: 'rotate', fingers: number, receiver: Callback<Rotate>): voidCancels listening for rotation events.

How to Develop

This 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, and cancel listening for mouse button events by calling off.

import { MouseEvent } from '@kit.InputKit';

let BUTTON_DOWN = 2;
let callback = (mouseEvent: MouseEvent) => {
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
    if(mouseEvent.action = BUTTON_DOWN){
      return true;// Callback triggered when the mouse button is pressed.
    }
    return false;
};

try {
  inputMonitor.on('mouse', (mouseEvent: MouseEvent) => {// Start to listen for mouse events.
    console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
    return false;
  });
} catch (error) {
  console.log(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// Callback triggered when the mouse button is pressed.
try {
  inputMonitor.off('mouse', callback);// Cancel listening for mouse events.
  console.log(`Monitor off success`);
} catch (error) {
  console.log(`Monitor off failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Input Kit (Multimodal Input)

harmony 鸿蒙Introduction to Input Kit

harmony 鸿蒙Combination Key Development

harmony 鸿蒙Input Device Development

harmony 鸿蒙Event Injection Development

harmony 鸿蒙Event Interception Development (C/C++)

harmony 鸿蒙Event Listening Development (C/C++)

harmony 鸿蒙Mouse Pointer Development

harmony 鸿蒙Shortcut Key Development

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/aj5EtY