openharmony 鸿蒙 inputmonitor-guidelines-sys

2026-08-25 浏览 (1)

Input Monitor Development (for System Applications Only)

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 common APIs for input monitor. For details, see @ohos.multimodalInput.inputMonitor (Input Monitor) (System API).

APIDescription
on(type: 'mouse', receiver: Callback<MouseEvent>): voidListens for mouse events.
on(type: 'touch', receiver: TouchEventReceiver): voidEnables listening 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): voidDisables 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 { inputMonitor } from '@kit.InputKit';
import { MouseEvent } from '@kit.InputKit';

@Entry
@Component
struct Index {
  build() {
    RelativeContainer() {
      Text()
        .onClick(() => {
          let BUTTON_DOWN = 2;
          let callback = (mouseEvent: MouseEvent) => {
            console.info(`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) => { // Enable listening for mouse events.
              console.info(`Monitor on success ${JSON.stringify(mouseEvent)}`);
              return false;
            });
          } catch (error) {
            console.error(`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.info(`Monitor off success`);
          } catch (error) {
            console.error(`Monitor off failed, error: ${JSON.stringify(error, ["code", "message"])}`);
          }
        })
    }
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 keypressed-guidelines

openharmony 鸿蒙 inputconsumer-guidelines-sys

openharmony 鸿蒙 shortkey-guidelines-sys

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 inputeventclient-guidelines-sys

openharmony 鸿蒙 inputdevice-guidelines

openharmony 鸿蒙 interceptor-guidelines

openharmony 鸿蒙 pointerstyle-guidelines

openharmony 鸿蒙 input-overview

openharmony 鸿蒙 monitor-guidelines

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