js-apis-inputeventclient.md 2.2 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.multimodalInput.inputEventClient (Key Injection)
E
ester.zhou 已提交
2

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

S
shawn_he 已提交
5
> **NOTE**
S
shawn_he 已提交
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.
S
shawn_he 已提交
8
>
S
shawn_he 已提交
9
> - The APIs provided by this module are system APIs.
E
ester.zhou 已提交
10 11 12

## Modules to Import

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

## inputEventClient.injectEvent

injectEvent({KeyEvent: KeyEvent}): void

S
shawn_he 已提交
21
Injects a key event. Currently, this API applies only to the **/KEYCODE_BACK** key (key value 2).
S
shawn_he 已提交
22

E
ester.zhou 已提交
23 24 25 26
**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator

**Parameters**

S
shawn_he 已提交
27 28 29
| Name      | Type                   | Mandatory  | Description       |
| -------- | --------------------- | ---- | --------- |
| KeyEvent | [KeyEvent](#keyevent) | Yes   | Information about the key event to inject.|
E
ester.zhou 已提交
30 31 32

**Example**

S
shawn_he 已提交
33
```js
S
shawn_he 已提交
34
try {
S
shawn_he 已提交
35
  let backKeyDown = {
E
ester.zhou 已提交
36 37 38 39
    isPressed: true,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
S
shawn_he 已提交
40
  }
S
shawn_he 已提交
41 42 43
  inputEventClient.injectEvent({ KeyEvent: backKeyDown });

  let backKeyUp = {
S
shawn_he 已提交
44 45 46 47 48
    isPressed: false,
    keyCode: 2,
    keyDownDuration: 0,
    isIntercepted: false
  };
S
shawn_he 已提交
49
  inputEventClient.injectEvent({ KeyEvent: backKeyUp });
S
shawn_he 已提交
50
} catch (error) {
S
shawn_he 已提交
51
  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
E
ester.zhou 已提交
52 53 54 55 56
}
```

## KeyEvent

S
shawn_he 已提交
57
Represents information about the key event to inject.
E
ester.zhou 已提交
58 59 60

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

S
shawn_he 已提交
61 62
| Name       | Type  | Readable  | Writable  | Description     |
| --------- | ------ | ---- | ---- | ------- |
S
shawn_he 已提交
63 64 65 66
| isPressed       | boolean | Yes   |  No| Whether the key is pressed.<br>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.<br>The value **true** indicates that the key event can be intercepted, and the value **false** indicates the opposite.|