• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimodalInput.inputEventClient (Input Event Injection) (System API)
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 '@kit.InputKit';
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
25Permission required: ohos.permission.INJECT_INPUT_EVENT
26
27**Parameters**
28
29| Name      | Type                   | Mandatory  | Description       |
30| -------- | --------------------- | ---- | --------- |
31| KeyEvent | [KeyEvent](#keyevent) | Yes   | Key event to inject.|
32
33**Error codes**
34
35For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
36
37| ID | Error Message            |
38| ---- | --------------------- |
39| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
40
41**Example**
42
43```js
44try {
45  let backKeyDown: inputEventClient.KeyEvent = {
46    isPressed: true,
47    keyCode: 2,
48    keyDownDuration: 0,
49    isIntercepted: false
50  }
51
52  class EventDown {
53    KeyEvent: inputEventClient.KeyEvent | null = null
54  }
55
56  let eventDown: EventDown = { KeyEvent: backKeyDown }
57  inputEventClient.injectEvent(eventDown);
58
59  let backKeyUp: inputEventClient.KeyEvent = {
60    isPressed: false,
61    keyCode: 2,
62    keyDownDuration: 0,
63    isIntercepted: false
64  };
65
66  class EventUp {
67    KeyEvent: inputEventClient.KeyEvent | null = null
68  }
69
70  let eventUp: EventUp = { KeyEvent: backKeyUp }
71  inputEventClient.injectEvent(eventUp);
72} catch (error) {
73  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
74}
75```
76
77## inputEventClient.injectKeyEvent<sup>11+</sup>
78
79injectKeyEvent(keyEvent: KeyEventData): void
80
81Injects key events (for both single keys and combination keys).
82
83**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
84
85Permission required: ohos.permission.INJECT_INPUT_EVENT
86
87**Parameters**
88
89| Name      | Type                   | Mandatory  | Description       |
90| -------- | --------------------- | ---- | --------- |
91| keyEvent | [KeyEventData](#keyeventdata11) | Yes   | Key event to inject.|
92
93**Error codes**
94
95For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
96
97| ID | Error Message            |
98| ---- | --------------------- |
99| 202  | SystemAPI permission error.  |
100| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
101
102**Example**
103
104```js
105try {
106  let backKeyDown: inputEventClient.KeyEvent = {
107    isPressed: true,
108    keyCode: 2,
109    keyDownDuration: 0,
110    isIntercepted: false
111  }
112
113  class EventDown {
114    keyEvent: inputEventClient.KeyEvent | null = null
115  }
116
117  let eventDown: EventDown = { keyEvent: backKeyDown }
118  inputEventClient.injectKeyEvent(eventDown);
119
120  let backKeyUp: inputEventClient.KeyEvent = {
121    isPressed: false,
122    keyCode: 2,
123    keyDownDuration: 0,
124    isIntercepted: false
125  };
126
127  class EventUp {
128    keyEvent: inputEventClient.KeyEvent | null = null
129  }
130
131  let eventUp: EventUp = { keyEvent: backKeyUp }
132  inputEventClient.injectKeyEvent(eventUp);
133} catch (error) {
134  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
135}
136```
137## inputEventClient.injectMouseEvent<sup>11+</sup>
138
139injectMouseEvent(mouseEvent: MouseEventData): void;
140
141Injects a mouse/touchpad event.
142
143**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
144
145Permission required: ohos.permission.INJECT_INPUT_EVENT
146
147**Parameters**
148
149| Name      | Type                   | Mandatory  | Description       |
150| -------- | --------------------- | ---- | --------- |
151| mouseEvent | [MouseEventData](#mouseeventdata11) | Yes   | Mouse/touchpad event to inject.|
152
153**Error codes**
154
155For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
156
157| ID | Error Message            |
158| ---- | --------------------- |
159| 202  | SystemAPI permission error.  |
160| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
161
162**Example**
163
164```js
165import { MouseEvent } from '@kit.InputKit';
166
167try {
168  let mouseButtonUpData: MouseEvent = {
169    id: 0,
170    deviceId: 1,
171    actionTime: 2,
172    screenId: 1,
173    windowId: 0,
174    action: 3,
175    screenX: 100,
176    screenY: 200,
177    windowX: 100,
178    windowY: 200,
179    rawDeltaX: 200,
180    rawDeltaY: 200,
181    button: 2,
182    pressedButtons: [2],
183    axes: [],
184    pressedKeys: [0],
185    ctrlKey: false,
186    altKey: false,
187    shiftKey: false,
188    logoKey: false,
189    fnKey: false,
190    capsLock: false,
191    numLock: false,
192    scrollLock: false,
193    toolType: 1,
194  }
195  let mouseButtonUp: inputEventClient.MouseEventData = {
196    mouseEvent: mouseButtonUpData
197  }
198  inputEventClient.injectMouseEvent(mouseButtonUp);
199
200  let mouseButtonDownData: MouseEvent = {
201    id: 0,
202    deviceId: 1,
203    actionTime: 2,
204    screenId: 1,
205    windowId: 0,
206    action: 2,
207    screenX: 100,
208    screenY: 200,
209    windowX: 100,
210    windowY: 200,
211    rawDeltaX: 200,
212    rawDeltaY: 200,
213    button: 2,
214    pressedButtons: [2],
215    axes: [],
216    pressedKeys: [0],
217    ctrlKey: false,
218    altKey: false,
219    shiftKey: false,
220    logoKey: false,
221    fnKey: false,
222    capsLock: false,
223    numLock: false,
224    scrollLock: false,
225    toolType: 1,
226  }
227  let mouseButtonDown: inputEventClient.MouseEventData = {
228    mouseEvent: mouseButtonDownData
229  };
230  inputEventClient.injectMouseEvent(mouseButtonDown);
231}
232
233catch (error) {
234  console.log(`Failed to inject MouseEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
235}
236
237```
238
239## inputEventClient.injectTouchEvent<sup>11+</sup>
240
241injectTouchEvent(touchEvent: TouchEventData): void
242
243Injects a touchscreen event.
244
245**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
246
247Permission required: ohos.permission.INJECT_INPUT_EVENT
248
249**Parameters**
250
251| Name      | Type                   | Mandatory  | Description       |
252| -------- | --------------------- | ---- | --------- |
253| touchEvent | [TouchEventData](#toucheventdata11) | Yes   | Touchscreen event to inject.|
254
255**Error codes**
256
257For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
258
259| ID | Error Message            |
260| ---- | --------------------- |
261| 202  | SystemAPI permission error.  |
262| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
263
264**Example**
265
266```js
267import { Touch, TouchEvent } from '@kit.InputKit';
268
269try {
270  let touchEvent: Touch = {
271    id: 1,
272    pressedTime: 1,
273    screenX: 0,
274    screenY: 0,
275    windowX: 0,
276    windowY: 0,
277    pressure: 0,
278    width: 0,
279    height: 0,
280    tiltX: 0,
281    tiltY: 0,
282    toolX: 0,
283    toolY: 0,
284    toolWidth: 0,
285    toolHeight: 0,
286    rawX: 0,
287    rawY: 0,
288    toolType: 0,
289  }
290
291  let touchEventUpData: TouchEvent = {
292    action: 1,
293    sourceType: 0,
294    touch: touchEvent,
295    touches: [],
296    id: 0,
297    deviceId: 0,
298    actionTime: 0,
299    screenId: 0,
300    windowId: 0
301  }
302  ;
303  let touchEventUp: inputEventClient.TouchEventData = {
304    touchEvent: touchEventUpData
305  }
306  inputEventClient.injectTouchEvent(touchEventUp);
307
308  let touchEventDownData: TouchEvent = {
309    action: 1,
310    sourceType: 0,
311    touch: touchEvent,
312    touches: [],
313    id: 0,
314    deviceId: 0,
315    actionTime: 0,
316    screenId: 0,
317    windowId: 0
318  }
319  ;
320  let touchEventDown: inputEventClient.TouchEventData = {
321    touchEvent: touchEventDownData
322  }
323  inputEventClient.injectTouchEvent(touchEventDown);
324} catch (error) {
325  console.log(`Failed to inject touchEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
326}
327```
328
329## inputEventClient.permitInjection<sup>12+</sup>
330
331permitInjection(result: boolean): void
332
333Specifies whether to authorize event injection.
334
335**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
336
337Permission required: ohos.permission.INJECT_INPUT_EVENT
338
339**Parameters**
340
341| Name   | Type   | Mandatory  | Description       |
342| -------- | ------  | ----   | --------- |
343| result   | boolean | Yes    | Authorization result. The value **true** indicates that event injection is allowed, and the value **false** indicates the opposite.|
344
345**Error codes**
346
347For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
348
349| ID | Error Message            |
350| ---- | --------------------- |
351| 202  | SystemAPI permission error.  |
352| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
353
354
355```ts
356import { inputEventClient } from '@kit.InputKit';
357
358try {
359  let result = true;
360  inputEventClient.permitInjection(result);
361}catch(error){
362  console.error("failed:" + JSON.stringify(error));
363}
364```
365
366## KeyEvent
367
368Defines the key event to inject.
369
370**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
371
372| Name       | Type  | Readable  | Writable  | Description     |
373| --------- | ------ | ---- | ---- | ------- |
374| 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.  |
375| keyCode         | number  | Yes   |  No| Keycode value. Currently, only the **KEYCODE_BACK** key is supported.|
376| keyDownDuration | number  | Yes   |  No| Duration for pressing a key, in μs.          |
377| 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.|
378
379## KeyEventData<sup>11+</sup>
380
381Defines the key event to inject.
382
383**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
384
385| Name       | Type  | Mandatory  | Description     |
386| --------- | ------ | ---- |  ------- |
387| keyEvent | [KeyEvent](#keyevent) | Yes   | Key event to inject.  |
388
389## MouseEventData<sup>11+</sup>
390
391Defines the mouse event data.
392
393**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
394
395| Name       | Type  | Readable  | Writable  | Description     |
396| --------- | ------ | ---- | ---- | ------- |
397| mouseEvent | [MouseEvent](js-apis-mouseevent.md#mouseevent) | Yes   |  No| Mouse event data.  |
398
399## TouchEventData<sup>11+</sup>
400
401Defines the touchscreen event data.
402
403**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator
404
405| Name       | Type  | Readable  | Writable  | Description     |
406| --------- | ------ | ---- | ---- | ------- |
407| touchEvent | [TouchEvent](js-apis-touchevent.md#touchevent) | Yes   |  No| Touchscreen event data  |
408