@ohos.multimodalInput.inputEventClient (Input Event Injection) (System API)

The inputEventClient module implements the input event injection capability.

NOTE

  • The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs provided by this module are system APIs.

Modules to Import

import inputEventClient from '@ohos.multimodalInput.inputEventClient';

inputEventClient.injectEvent

injectEvent({KeyEvent: KeyEvent}): void

Injects keys (including single keys and combination keys).

System capability: SystemCapability.MultimodalInput.Input.InputSimulator

Parameters

Name Type Mandatory Description
KeyEvent KeyEvent Yes Key event to inject.

Example

try {
  let backKeyDown: inputEventClient.KeyEvent = {
    isPressed: true,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
  }

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

  let eventDown: EventDown = { KeyEvent: backKeyDown }
  inputEventClient.injectEvent(eventDown);

  let backKeyUp: inputEventClient.KeyEvent = {
    isPressed: false,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
  };

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

  let eventUp: EventUp = { KeyEvent: backKeyUp }
  inputEventClient.injectEvent(eventUp);
} catch (error) {
  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputEventClient.injectKeyEvent11+

injectKeyEvent(keyEvent: KeyEventData): void

Injects key events (for both single keys and combination keys).

System capability: SystemCapability.MultimodalInput.Input.InputSimulator

Parameters

Name Type Mandatory Description
keyEvent KeyEventData Yes Key event to inject.

Example

try {
  let backKeyDown: inputEventClient.KeyEvent = {
    isPressed: true,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
  }

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

  let eventDown: EventDown = { KeyEvent: backKeyDown }
  inputEventClient.injectKeyEvent(eventDown);

  let backKeyUp: inputEventClient.KeyEvent = {
    isPressed: false,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
  };

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

  let eventUp: EventUp = { KeyEvent: backKeyUp }
  inputEventClient.injectKeyEvent(eventUp);
} catch (error) {
  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputEventClient.injectMouseEvent11+

injectMouseEvent(mouseEvent: MouseEventData): void;

Injects a mouse/touchpad event.

System capability: SystemCapability.MultimodalInput.Input.InputSimulator

Parameters

Name Type Mandatory Description
mouseEvent MouseEventData Yes Mouse/touchpad event to inject.

Example

import mouseEvent from '@ohos.multimodalInput.mouseEvent'

try {
  let mouseButtonUpData: mouseEvent.MouseEvent = {
    id: 0,
    deviceId: 1,
    actionTime: 2,
    screenId: 1,
    windowId: 0,
    action: 3,
    screenX: 100,
    screenY: 200,
    windowX: 100,
    windowY: 200,
    rawDeltaX: 200,
    rawDeltaY: 200,
    button: 2,
    pressedButtons: [2],
    axes: [],
    pressedKeys: [0],
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    logoKey: false,
    fnKey: false,
    capsLock: false,
    numLock: false,
    scrollLock: false,
    toolType: 1,
  }
  let mouseButtonUp: inputEventClient.MouseEventData = {
    mouseEvent: mouseButtonUpData
  }
  inputEventClient.injectMouseEvent(mouseButtonUp);

  let mouseButtonDownData: mouseEvent.MouseEvent = {
    id: 0,
    deviceId: 1,
    actionTime: 2,
    screenId: 1,
    windowId: 0,
    action: 2,
    screenX: 100,
    screenY: 200,
    windowX: 100,
    windowY: 200,
    rawDeltaX: 200,
    rawDeltaY: 200,
    button: 2,
    pressedButtons: [2],
    axes: [],
    pressedKeys: [0],
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    logoKey: false,
    fnKey: false,
    capsLock: false,
    numLock: false,
    scrollLock: false,
    toolType: 1,
  }
  let mouseButtonDown: inputEventClient.MouseEventData = {
    mouseEvent: mouseButtonDownData
  };
  inputEventClient.injectMouseEvent(mouseButtonDown);
}

catch (error) {
  console.log(`Failed to inject MouseEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputEventClient.injectTouchEvent11+

injectTouchEvent(touchEvent: TouchEventData): void

Injects a touchscreen event.

System capability: SystemCapability.MultimodalInput.Input.InputSimulator

Parameters

Name Type Mandatory Description
touchEvent TouchEventData Yes Touchscreen event to inject.

Example

import touchEvent from '@ohos.multimodalInput.touchEvent'

try {
  let touchEvent: touchEvent.Touch = {
    id: 1,
    pressedTime: 1,
    screenX: 0,
    screenY: 0,
    windowX: 0,
    windowY: 0,
    pressure: 0,
    width: 0,
    height: 0,
    tiltX: 0,
    tiltY: 0,
    toolX: 0,
    toolY: 0,
    toolWidth: 0,
    toolHeight: 0,
    rawX: 0,
    rawY: 0,
    toolType: 0,
  }

  let touchEventUpData: touchEvent.TouchEvent = {
    action: 1,
    sourceType: 0,
    touch: touchEvent,
    touches: [],
    id: 0,
    deviceId: 0,
    actionTime: 0,
    screenId: 0,
    windowId: 0
  }
  ;
  let touchEventUp: inputEventClient.TouchEventData = {
    touchEvent: touchEventUpData
  }
  inputEventClient.injectTouchEvent(touchEventUp);

  let touchEventDownData: touchEvent.TouchEvent = {
    action: 1,
    sourceType: 0,
    touch: touchEvent,
    touches: [],
    id: 0,
    deviceId: 0,
    actionTime: 0,
    screenId: 0,
    windowId: 0
  }
  ;
  let touchEventDown: inputEventClient.TouchEventData = {
    touchEvent: touchEventDownData
  }
  inputEventClient.injectTouchEvent(touchEventDown);
} catch (error) {
  console.log(`Failed to inject touchEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

KeyEvent

Defines the key event to inject.

System capability: SystemCapability.MultimodalInput.Input.InputSimulator

Name Type Readable Writable Description
isPressed boolean Yes No Whether the key is pressed.
The value true indicates that the key is pressed, and the value false indicates the opposite.
keyCode number Yes No Keycode value. Currently, only the KEYCODE_BACK key is supported.
keyDownDuration number Yes No Duration for pressing a key, in μs.
isIntercepted boolean Yes No Whether the key event can be intercepted.
The value true indicates that the key event can be intercepted, and the value false indicates the opposite.

KeyEventData11+

Defines the key event to inject.

System capability: SystemCapability.MultimodalInput.Input.InputSimulator

Name Type Mandatory Description
keyEvent KeyEvent Yes Key event to inject.

MouseEventData11+

Defines the mouse event data.

System capability: SystemCapability.MultimodalInput.Input.InputSimulator

Name Type Readable Writable Description
mouseEvent MouseEvent Yes No Mouse event data.

TouchEventData11+

Defines the touchscreen event data.

System capability: SystemCapability.MultimodalInput.Input.InputSimulator

Name Type Readable Writable Description
touchEvent TouchEvent Yes No Touchscreen event data