• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputEventClient (Input Event Injection)
2
3The **inputEventClient** module implements the input event injection capability.
4
5> **NOTE**
6>
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.
8>
9> - The APIs provided by this module are system APIs.
10
11## Modules to Import
12
13```js
14import inputEventClient from '@ohos.multimodalInput.inputEventClient';
15```
16
17## inputEventClient.injectEvent
18
19injectEvent({KeyEvent: KeyEvent}): void
20
21Injects keys (including single keys and combination keys).
22
23**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
24
25**Parameters**
26
27| Name      | Type                   | Mandatory  | Description       |
28| -------- | --------------------- | ---- | --------- |
29| KeyEvent | [KeyEvent](#keyevent) | Yes   | Key event to inject.|
30
31**Example**
32
33```js
34try {
35  let backKeyDown: inputEventClient.KeyEvent = {
36    isPressed: true,
37    keyCode: 2,
38    keyDownDuration: 0,
39    isIntercepted: false
40  }
41
42  class EventDown {
43    KeyEvent: inputEventClient.KeyEvent | null = null
44  }
45
46  let eventDown: EventDown = { KeyEvent: backKeyDown }
47  inputEventClient.injectEvent(eventDown);
48
49  let backKeyUp: inputEventClient.KeyEvent = {
50    isPressed: false,
51    keyCode: 2,
52    keyDownDuration: 0,
53    isIntercepted: false
54  };
55
56  class EventUp {
57    KeyEvent: inputEventClient.KeyEvent | null = null
58  }
59
60  let eventUp: EventUp = { KeyEvent: backKeyUp }
61  inputEventClient.injectEvent(eventUp);
62} catch (error) {
63  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
64}
65```
66## inputEventClient.injectKeyEvent<sup>11+</sup>
67
68injectKeyEvent(keyEvent: KeyEventData): void
69
70Injects key events (for both single keys and combination keys).
71
72**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
73
74**Parameters**
75
76| Name      | Type                   | Mandatory  | Description       |
77| -------- | --------------------- | ---- | --------- |
78| keyEvent | [KeyEventData](#keyeventdata11) | Yes   | Key event to inject.|
79
80**Example**
81
82```js
83try {
84  let backKeyDown: inputEventClient.KeyEvent = {
85    isPressed: true,
86    keyCode: 2,
87    keyDownDuration: 0,
88    isIntercepted: false
89  }
90
91  class EventDown {
92    KeyEvent: inputEventClient.KeyEvent | null = null
93  }
94
95  let eventDown: EventDown = { KeyEvent: backKeyDown }
96  inputEventClient.injectKeyEvent(eventDown);
97
98  let backKeyUp: inputEventClient.KeyEvent = {
99    isPressed: false,
100    keyCode: 2,
101    keyDownDuration: 0,
102    isIntercepted: false
103  };
104
105  class EventUp {
106    KeyEvent: inputEventClient.KeyEvent | null = null
107  }
108
109  let eventUp: EventUp = { KeyEvent: backKeyUp }
110  inputEventClient.injectKeyEvent(eventUp);
111} catch (error) {
112  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
113}
114```
115## inputEventClient.injectMouseEvent<sup>11+</sup>
116
117injectMouseEvent(mouseEvent: MouseEventData): void;
118
119Injects a mouse/touchpad event.
120
121**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
122
123**Parameters**
124
125| Name      | Type                   | Mandatory  | Description       |
126| -------- | --------------------- | ---- | --------- |
127| mouseEvent | [MouseEventData](#mouseeventdata11) | Yes   | Mouse/touchpad event to inject.|
128
129**Example**
130
131```js
132import mouseEvent from '@ohos.multimodalInput.mouseEvent'
133
134try {
135  let mouseButtonUpData: mouseEvent.MouseEvent = {
136    id: 0,
137    deviceId: 1,
138    actionTime: 2,
139    screenId: 1,
140    windowId: 0,
141    action: 3,
142    screenX: 100,
143    screenY: 200,
144    windowX: 100,
145    windowY: 200,
146    rawDeltaX: 200,
147    rawDeltaY: 200,
148    button: 2,
149    pressedButtons: [2],
150    axes: [],
151    pressedKeys: [0],
152    ctrlKey: false,
153    altKey: false,
154    shiftKey: false,
155    logoKey: false,
156    fnKey: false,
157    capsLock: false,
158    numLock: false,
159    scrollLock: false,
160    toolType: 1,
161  }
162  let mouseButtonUp: inputEventClient.MouseEventData = {
163    mouseEvent: mouseButtonUpData
164  }
165  inputEventClient.injectMouseEvent(mouseButtonUp);
166
167  let mouseButtonDownData: mouseEvent.MouseEvent = {
168    id: 0,
169    deviceId: 1,
170    actionTime: 2,
171    screenId: 1,
172    windowId: 0,
173    action: 2,
174    screenX: 100,
175    screenY: 200,
176    windowX: 100,
177    windowY: 200,
178    rawDeltaX: 200,
179    rawDeltaY: 200,
180    button: 2,
181    pressedButtons: [2],
182    axes: [],
183    pressedKeys: [0],
184    ctrlKey: false,
185    altKey: false,
186    shiftKey: false,
187    logoKey: false,
188    fnKey: false,
189    capsLock: false,
190    numLock: false,
191    scrollLock: false,
192    toolType: 1,
193  }
194  let mouseButtonDown: inputEventClient.MouseEventData = {
195    mouseEvent: mouseButtonDownData
196  };
197  inputEventClient.injectMouseEvent(mouseButtonDown);
198}
199
200catch (error) {
201  console.log(`Failed to inject MouseEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
202}
203
204```
205
206## inputEventClient.injectTouchEvent<sup>11+</sup>
207
208injectTouchEvent(touchEvent: TouchEventData): void
209
210Injects a touchscreen event.
211
212**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
213
214**Parameters**
215
216| Name      | Type                   | Mandatory  | Description       |
217| -------- | --------------------- | ---- | --------- |
218| touchEvent | [TouchEventData](#toucheventdata11) | Yes   | Touchscreen event to inject.|
219
220**Example**
221
222```js
223import touchEvent from '@ohos.multimodalInput.touchEvent'
224
225try {
226  let touchEvent: touchEvent.Touch = {
227    id: 1,
228    pressedTime: 1,
229    screenX: 0,
230    screenY: 0,
231    windowX: 0,
232    windowY: 0,
233    pressure: 0,
234    width: 0,
235    height: 0,
236    tiltX: 0,
237    tiltY: 0,
238    toolX: 0,
239    toolY: 0,
240    toolWidth: 0,
241    toolHeight: 0,
242    rawX: 0,
243    rawY: 0,
244    toolType: 0,
245  }
246
247  let touchEventUpData: touchEvent.TouchEvent = {
248    action: 1,
249    sourceType: 0,
250    touch: touchEvent,
251    touches: [],
252    id: 0,
253    deviceId: 0,
254    actionTime: 0,
255    screenId: 0,
256    windowId: 0
257  }
258  ;
259  let touchEventUp: inputEventClient.TouchEventData = {
260    touchEvent: touchEventUpData
261  }
262  inputEventClient.injectTouchEvent(touchEventUp);
263
264  let touchEventDownData: touchEvent.TouchEvent = {
265    action: 1,
266    sourceType: 0,
267    touch: touchEvent,
268    touches: [],
269    id: 0,
270    deviceId: 0,
271    actionTime: 0,
272    screenId: 0,
273    windowId: 0
274  }
275  ;
276  let touchEventDown: inputEventClient.TouchEventData = {
277    touchEvent: touchEventDownData
278  }
279  inputEventClient.injectTouchEvent(touchEventDown);
280} catch (error) {
281  console.log(`Failed to inject touchEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
282}
283```
284
285## KeyEvent
286
287Defines the key event to inject.
288
289**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
290
291| Name       | Type  | Readable  | Writable  | Description     |
292| --------- | ------ | ---- | ---- | ------- |
293| 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.  |
294| keyCode         | number  | Yes   |  No| Keycode value. Currently, only the **KEYCODE_BACK** key is supported.|
295| keyDownDuration | number  | Yes   |  No| Duration for pressing a key, in μs.          |
296| 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.|
297
298## KeyEventData<sup>11+</sup>
299
300Defines the key event to inject.
301
302**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
303
304| Name       | Type  | Mandatory  | Description     |
305| --------- | ------ | ---- |  ------- |
306| keyEvent | [KeyEvent](#keyevent) | Yes   | Key event to inject.  |
307
308## MouseEventData<sup>11+</sup>
309
310Defines the mouse event data.
311
312**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
313
314| Name       | Type  | Readable  | Writable  | Description     |
315| --------- | ------ | ---- | ---- | ------- |
316| mouseEvent | [MouseEvent](../apis/js-apis-mouseevent.md) | Yes   |  No| Mouse event data.  |
317
318## TouchEventData<sup>11+</sup>
319
320Defines the touchscreen event data.
321
322**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
323
324| Name       | Type  | Readable  | Writable  | Description     |
325| --------- | ------ | ---- | ---- | ------- |
326| touchEvent | [TouchEvent](../apis/js-apis-touchevent.md) | Yes   |  No| Touchscreen event data  |
327