js-apis-inputeventclient.md 1.9 KB
Newer Older
S
shawn_he 已提交
1
# Key Injection
E
ester.zhou 已提交
2

S
shawn_he 已提交
3
The Key Injection module implements injection of key events.
E
ester.zhou 已提交
4

S
shawn_he 已提交
5
> **NOTE**<br>
E
ester.zhou 已提交
6
>
S
shawn_he 已提交
7
> - 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.
E
ester.zhou 已提交
8
>
S
shawn_he 已提交
9
> - The APIs of this module are system APIs and cannot be called by third-party applications.
E
ester.zhou 已提交
10 11 12 13 14


## Modules to Import


S
shawn_he 已提交
15
```js
E
ester.zhou 已提交
16 17 18 19 20 21 22 23
import inputEventClient from '@ohos.multimodalInput.inputEventClient';
```


## inputEventClient.injectEvent

injectEvent({KeyEvent: KeyEvent}): void

S
shawn_he 已提交
24
Injects a key event.
E
ester.zhou 已提交
25

S
shawn_he 已提交
26 27
This is a system API.

E
ester.zhou 已提交
28 29 30 31
**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator

**Parameters**

S
shawn_he 已提交
32 33 34
| Name      | Type                   | Mandatory  | Description       |
| -------- | --------------------- | ---- | --------- |
| KeyEvent | [KeyEvent](#keyevent) | Yes   | Information about the key event to inject.|
E
ester.zhou 已提交
35 36 37

**Example**

S
shawn_he 已提交
38
```js
S
shawn_he 已提交
39 40
try {
  var keyEvent = {
E
ester.zhou 已提交
41 42 43 44
    isPressed: true,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
S
shawn_he 已提交
45 46 47 48 49 50 51 52 53 54 55
  }
  inputEventClient.injectKeyEvent({ KeyEvent: keyEvent });
  var keyEvent1 = {
    isPressed: false,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
  };
  inputEventClient.injectKeyEvent({ KeyEvent: keyEvent1 });
} catch (error) {
  console.info("injectKeyEvent " + error.code + " " + error.message);
E
ester.zhou 已提交
56 57 58 59 60 61
}
```


## KeyEvent

S
shawn_he 已提交
62
Defines the information about the key event to inject.
E
ester.zhou 已提交
63 64 65

**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator

S
shawn_he 已提交
66 67 68 69 70
**Parameters**

| Name             | Type     | Mandatory  | Description       |
| --------------- | ------- | ---- | --------- |
| isPressed       | boolean | Yes   | Whether the key is pressed.   |
S
shawn_he 已提交
71 72 73
| keyCode         | number  | Yes   | Key code.     |
| keyDownDuration | number  | Yes   | Duration within which the key is pressed. |
| isIntercepted   | boolean | Yes   | Whether the key can be intercepted.|