1# @ohos.multimodalInput.inputEventClient (输入事件注入)(系统接口)
2
3输入事件注入模块,提供输入事件注入能力。
4
5> **说明:**
6>
7> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> - 本模块接口为系统接口。
10
11## 导入模块
12
13```js
14import { inputEventClient } from '@kit.InputKit';
15```
16
17## inputEventClient.injectEvent
18
19injectEvent({KeyEvent: KeyEvent}): void
20
21按键(包括单个按键和组合键)注入。
22
23**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
24
25**需要权限:** ohos.permission.INJECT_INPUT_EVENT
26
27**参数:**
28
29| 参数名       | 类型                    | 必填   | 说明        |
30| -------- | --------------------- | ---- | --------- |
31| KeyEvent | [KeyEvent](#keyevent) | 是    | 按键注入描述信息。 |
32
33**错误码**:
34
35以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
36
37| 错误码ID  | 错误信息             |
38| ---- | --------------------- |
39| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
40
41**示例:**
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## inputEventClient.injectKeyEvent<sup>11+</sup>
77
78injectKeyEvent(keyEvent: KeyEventData): void
79
80按键(包括单个按键和组合键)事件注入。
81
82**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
83
84**需要权限:** ohos.permission.INJECT_INPUT_EVENT
85
86**参数:**
87
88| 参数名       | 类型                    | 必填   | 说明        |
89| -------- | --------------------- | ---- | --------- |
90| keyEvent | [KeyEventData](#keyeventdata11) | 是    | 按键事件注入描述信息。 |
91
92**错误码**:
93
94以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
95
96| 错误码ID  | 错误信息             |
97| ---- | --------------------- |
98| 202  | SystemAPI permission error.  |
99| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
100
101**示例:**
102
103```js
104try {
105  let backKeyDown: inputEventClient.KeyEvent = {
106    isPressed: true,
107    keyCode: 2,
108    keyDownDuration: 0,
109    isIntercepted: false
110  }
111
112  class EventDown {
113    KeyEvent: inputEventClient.KeyEvent | null = null
114  }
115
116  let eventDown: EventDown = { KeyEvent: backKeyDown }
117  inputEventClient.injectKeyEvent(eventDown);
118
119  let backKeyUp: inputEventClient.KeyEvent = {
120    isPressed: false,
121    keyCode: 2,
122    keyDownDuration: 0,
123    isIntercepted: false
124  };
125
126  class EventUp {
127    KeyEvent: inputEventClient.KeyEvent | null = null
128  }
129
130  let eventUp: EventUp = { KeyEvent: backKeyUp }
131  inputEventClient.injectKeyEvent(eventUp);
132} catch (error) {
133  console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
134}
135```
136## inputEventClient.injectMouseEvent<sup>11+</sup>
137
138injectMouseEvent(mouseEvent: MouseEventData): void;
139
140鼠标/触摸板事件注入。
141
142**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
143
144**需要权限:** ohos.permission.INJECT_INPUT_EVENT
145
146**参数:**
147
148| 参数名       | 类型                    | 必填   | 说明        |
149| -------- | --------------------- | ---- | --------- |
150| mouseEvent | [MouseEventData](#mouseeventdata11) | 是    | 鼠标/触摸板事件注入描述信息。 |
151
152**错误码**:
153
154以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
155
156| 错误码ID  | 错误信息             |
157| ---- | --------------------- |
158| 202  | SystemAPI permission error.  |
159| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
160
161**示例:**
162
163```js
164import { MouseEvent } from '@kit.InputKit';
165
166try {
167  let mouseButtonUpData: mouseEvent.MouseEvent = {
168    id: 0,
169    deviceId: 1,
170    actionTime: 2,
171    screenId: 1,
172    windowId: 0,
173    action: 3,
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 mouseButtonUp: inputEventClient.MouseEventData = {
195    mouseEvent: mouseButtonUpData
196  }
197  inputEventClient.injectMouseEvent(mouseButtonUp);
198
199  let mouseButtonDownData: mouseEvent.MouseEvent = {
200    id: 0,
201    deviceId: 1,
202    actionTime: 2,
203    screenId: 1,
204    windowId: 0,
205    action: 2,
206    screenX: 100,
207    screenY: 200,
208    windowX: 100,
209    windowY: 200,
210    rawDeltaX: 200,
211    rawDeltaY: 200,
212    button: 2,
213    pressedButtons: [2],
214    axes: [],
215    pressedKeys: [0],
216    ctrlKey: false,
217    altKey: false,
218    shiftKey: false,
219    logoKey: false,
220    fnKey: false,
221    capsLock: false,
222    numLock: false,
223    scrollLock: false,
224    toolType: 1,
225  }
226  let mouseButtonDown: inputEventClient.MouseEventData = {
227    mouseEvent: mouseButtonDownData
228  };
229  inputEventClient.injectMouseEvent(mouseButtonDown);
230}
231
232catch (error) {
233  console.log(`Failed to inject MouseEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
234}
235
236```
237
238## inputEventClient.injectTouchEvent<sup>11+</sup>
239
240injectTouchEvent(touchEvent: TouchEventData): void
241
242触摸屏事件注入。
243
244**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
245
246**需要权限:** ohos.permission.INJECT_INPUT_EVENT
247
248**参数:**
249
250| 参数名       | 类型                    | 必填   | 说明        |
251| -------- | --------------------- | ---- | --------- |
252| touchEvent | [TouchEventData](#toucheventdata11) | 是    | 触摸屏事件注入描述信息。 |
253
254**错误码**:
255
256以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
257
258| 错误码ID  | 错误信息             |
259| ---- | --------------------- |
260| 202  | SystemAPI permission error.  |
261| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
262
263**示例:**
264
265```js
266import { TouchEvent } from '@kit.InputKit';
267
268try {
269  let touchEvent: touchEvent.Touch = {
270    id: 1,
271    pressedTime: 1,
272    screenX: 0,
273    screenY: 0,
274    windowX: 0,
275    windowY: 0,
276    pressure: 0,
277    width: 0,
278    height: 0,
279    tiltX: 0,
280    tiltY: 0,
281    toolX: 0,
282    toolY: 0,
283    toolWidth: 0,
284    toolHeight: 0,
285    rawX: 0,
286    rawY: 0,
287    toolType: 0,
288  }
289
290  let touchEventUpData: touchEvent.TouchEvent = {
291    action: 1,
292    sourceType: 0,
293    touch: touchEvent,
294    touches: [],
295    id: 0,
296    deviceId: 0,
297    actionTime: 0,
298    screenId: 0,
299    windowId: 0
300  }
301  ;
302  let touchEventUp: inputEventClient.TouchEventData = {
303    touchEvent: touchEventUpData
304  }
305  inputEventClient.injectTouchEvent(touchEventUp);
306
307  let touchEventDownData: touchEvent.TouchEvent = {
308    action: 1,
309    sourceType: 0,
310    touch: touchEvent,
311    touches: [],
312    id: 0,
313    deviceId: 0,
314    actionTime: 0,
315    screenId: 0,
316    windowId: 0
317  }
318  ;
319  let touchEventDown: inputEventClient.TouchEventData = {
320    touchEvent: touchEventDownData
321  }
322  inputEventClient.injectTouchEvent(touchEventDown);
323} catch (error) {
324  console.log(`Failed to inject touchEvent, error: ${JSON.stringify(error, [`code`, `message`])}`);
325}
326```
327
328## inputEventClient.permitInjection<sup>12+</sup>
329
330permitInjection(result: boolean): void
331
332允许事件注入权限。
333
334**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
335
336**需要权限:** ohos.permission.INJECT_INPUT_EVENT
337
338**参数:**
339
340| 参数名    | 类型    | 必填   | 说明        |
341| -------- | ------  | ----   | --------- |
342| result   | boolean | 是     | 授权结果(true表示:允许事件注入,false表示:不允许事件注入)。 |
343
344**错误码**:
345
346以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
347
348| 错误码ID  | 错误信息             |
349| ---- | --------------------- |
350| 202  | SystemAPI permission error.  |
351| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. |
352
353
354```ts
355import { inputEventClient } from '@kit.InputKit';
356
357try {
358  let result = true;
359  inputEventClient.permitInjection(result);
360}catch(error){
361  console.error("failed:" + JSON.stringify(error));
362}
363```
364
365## KeyEvent
366
367按键注入描述信息。
368
369**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
370
371| 名称        | 类型   | 可读   | 可写   | 说明      |
372| --------- | ------ | ---- | ---- | ------- |
373| isPressed       | boolean | 是    |  否 | 按键是否按下。<br>true表示按键按下,false表示按键抬起。   |
374| keyCode         | number  | 是    |  否 | 按键键码值。当前仅支持返回键/KEYCODE_BACK键。 |
375| keyDownDuration | number  | 是    |  否 | 按键按下持续时间,单位为微秒(μs)。           |
376| isIntercepted   | boolean | 是    |  否 | 按键是否可以被拦截。<br>true表示可以被拦截,false表示不可被拦截。 |
377
378## KeyEventData<sup>11+</sup>
379
380按键注入描述信息。
381
382**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
383
384| 名称        | 类型   | 必填   | 说明      |
385| --------- | ------ | ---- |  ------- |
386| keyEvent | [KeyEvent](#keyevent) | 是    | 按键注入描述信息。   |
387
388## MouseEventData<sup>11+</sup>
389
390鼠标注入描述信息。
391
392**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
393
394| 名称        | 类型   | 可读   | 可写   | 说明      |
395| --------- | ------ | ---- | ---- | ------- |
396| mouseEvent | [MouseEvent](js-apis-mouseevent.md#mouseevent) | 是    |  否 | 鼠标注入描述信息。   |
397
398## TouchEventData<sup>11+</sup>
399
400触摸屏注入描述信息。
401
402**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator
403
404| 名称        | 类型   | 可读   | 可写   | 说明      |
405| --------- | ------ | ---- | ---- | ------- |
406| touchEvent | [TouchEvent](js-apis-touchevent.md#touchevent) | 是    |  否 | 触摸屏注入描述信息。   |