• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputEventClient (Key Event Injection)
2
3The **inputEventClient** module implements injection of key events.
4
5> **NOTE**
6> - 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.
7> - The APIs provided by this module are system APIs.
8
9
10## Modules to Import
11
12
13```js
14import inputEventClient from '@ohos.multimodalInput.inputEventClient';
15```
16
17
18## inputEventClient.injectEvent
19
20injectEvent({KeyEvent: KeyEvent}): void
21
22Injects a key event. Currently, key injection is supported only for the **Back** key (key value 2).
23
24**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
25
26**Parameters**
27
28| Name      | Type                   | Mandatory  | Description       |
29| -------- | --------------------- | ---- | --------- |
30| KeyEvent | [KeyEvent](#keyevent) | Yes   | Information about the key event to inject.|
31
32**Example**
33
34```js
35try {
36  let backKeyDown = {
37    isPressed: true,
38    keyCode: 2,
39    keyDownDuration: 0,
40    isIntercepted: false
41  }
42  inputEventClient.injectEvent({ KeyEvent: backKeyDown });
43
44  let backKeyUp = {
45    isPressed: false,
46    keyCode: 2,
47    keyDownDuration: 0,
48    isIntercepted: false
49  };
50  inputEventClient.injectEvent({ KeyEvent: backKeyUp });
51} catch (error) {
52  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
53}
54```
55
56
57## KeyEvent
58
59Represents information about the key event to inject.
60
61**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
62
63| Name       | Type  | Readable  | Writable  | Description     |
64| --------- | ------ | ---- | ---- | ------- |
65| isPressed       | boolean | Yes   |  No| Whether the key is pressed.              |
66| keyCode         | number  | Yes   |  No| Key value. Currently, only the **Back** key is supported.|
67| keyDownDuration | number  | Yes   |  No| Duration within which the key is pressed.          |
68| isIntercepted   | boolean | Yes   |  No| Whether the key can be intercepted.        |
69