harmony 鸿蒙Event Injection Development

  • 2025-06-06
  • 浏览 (3)

Event Injection Development

When to Use

The inputEventClient module provides the function of injecting input events, such as mouse click events and combination key events. For example, if you need to verify the combination key function of an application, you can listen for combination key events to serve that purpose.

Modules to Import

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

Available APIs

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

API Description
injectEvent({KeyEvent: KeyEvent}): void Injects keys (including single keys and combination keys).
injectMouseEvent(mouseEvent: MouseEventData): void Injects a mouse/touchpad event.
injectTouchEvent(touchEvent: TouchEventData): void Injects a touchscreen event.

How to Develop

Assume that an application calls the Home key to return to the home screen. When the Home key is pressed, check whether injectEvent is called to inject the Home key to determine if the Home key takes effect.

try {
  let backKeyDown: inputEventClient.KeyEvent = {
    isPressed: true,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
  }// Home key pressing event

  class EventDown {
    KeyEvent: inputEventClient.KeyEvent|null = null
  }

  let eventDown: EventDown = { KeyEvent: backKeyDown }
  inputEventClient.injectEvent(eventDown);// Inject the Home key pressing event.

  let backKeyUp: inputEventClient.KeyEvent = {
    isPressed: false,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
  };// Home key release event.

  class EventUp {
    KeyEvent: inputEventClient.KeyEvent|null = null
  }

  let eventUp: EventUp = { KeyEvent: backKeyUp }
  inputEventClient.injectEvent(eventUp);// Inject the Home key release event and check whether the Home key function takes effect and whether the application returns to the home screen.
} catch (error) {
  console.log(`Failed to inject KeyEvent, 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 鸿蒙Input Monitor Development

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

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

harmony 鸿蒙Mouse Pointer Development

harmony 鸿蒙Shortcut Key Development

0  赞