openharmony 鸿蒙 inputeventclient-guidelines-sys

2026-08-25 浏览 (1)

Event Injection Development (for System Applications Only)

When to Use

The inputEventClient module enables an application to listen for injected 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 common APIs for event injection. For details, see @ohos.multimodalInput.inputEventClient (Input Event Injection) (System API).

APIDescription
injectEvent({KeyEvent: KeyEvent}): voidInjects keys (including single keys and combination keys).
injectMouseEvent(mouseEvent: MouseEventData): voidInjects a mouse/touchpad event.
injectTouchEvent(touchEvent: TouchEventData): voidInjects 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.

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

@Entry
@Component
struct Index {
  build() {
    RelativeContainer() {
      Text()
        .onClick(() => {
          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.error(`Failed to inject KeyEvent, error: ${JSON.stringify(error, ["code", "message"])}`);
          }
        })
    }
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 keypressed-guidelines

openharmony 鸿蒙 inputconsumer-guidelines-sys

openharmony 鸿蒙 shortkey-guidelines-sys

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 inputdevice-guidelines

openharmony 鸿蒙 interceptor-guidelines

openharmony 鸿蒙 pointerstyle-guidelines

openharmony 鸿蒙 input-overview

openharmony 鸿蒙 inputmonitor-guidelines-sys

openharmony 鸿蒙 monitor-guidelines

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