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## inputEventClient.injectKeyEvent<sup>11+</sup> 77 78injectKeyEvent(keyEvent: KeyEventData): void 79 80Injects key events (for both single keys and combination keys). 81 82**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator 83 84Permission required: ohos.permission.INJECT_INPUT_EVENT 85 86**Parameters** 87 88| Name | Type | Mandatory | Description | 89| -------- | --------------------- | ---- | --------- | 90| keyEvent | [KeyEventData](#keyeventdata11) | Yes | Key event to inject.| 91 92**Error codes** 93 94For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 95 96| ID | Error Message | 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**Example** 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 140Injects a mouse/touchpad event. 141 142**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator 143 144Permission required: ohos.permission.INJECT_INPUT_EVENT 145 146**Parameters** 147 148| Name | Type | Mandatory | Description | 149| -------- | --------------------- | ---- | --------- | 150| mouseEvent | [MouseEventData](#mouseeventdata11) | Yes | Mouse/touchpad event to inject.| 151 152**Error codes** 153 154For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 155 156| ID | Error Message | 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**Example** 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 242Injects a touchscreen event. 243 244**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator 245 246Permission required: ohos.permission.INJECT_INPUT_EVENT 247 248**Parameters** 249 250| Name | Type | Mandatory | Description | 251| -------- | --------------------- | ---- | --------- | 252| touchEvent | [TouchEventData](#toucheventdata11) | Yes | Touchscreen event to inject.| 253 254**Error codes** 255 256For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 257 258| ID | Error Message | 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**Example** 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 332Specifies whether to authorize event injection. 333 334**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator 335 336Permission required: ohos.permission.INJECT_INPUT_EVENT 337 338**Parameters** 339 340| Name | Type | Mandatory | Description | 341| -------- | ------ | ---- | --------- | 342| result | boolean | Yes | Authorization result. The value **true** indicates that event injection is allowed, and the value **false** indicates the opposite.| 343 344**Error codes** 345 346For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 347 348| ID | Error Message | 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 367Defines the key event to inject. 368 369**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator 370 371| Name | Type | Readable | Writable | Description | 372| --------- | ------ | ---- | ---- | ------- | 373| 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. | 374| keyCode | number | Yes | No| Keycode value. Currently, only the **KEYCODE_BACK** key is supported.| 375| keyDownDuration | number | Yes | No| Duration for pressing a key, in μs. | 376| 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.| 377 378## KeyEventData<sup>11+</sup> 379 380Defines the key event to inject. 381 382**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator 383 384| Name | Type | Mandatory | Description | 385| --------- | ------ | ---- | ------- | 386| keyEvent | [KeyEvent](#keyevent) | Yes | Key event to inject. | 387 388## MouseEventData<sup>11+</sup> 389 390Defines the mouse event data. 391 392**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator 393 394| Name | Type | Readable | Writable | Description | 395| --------- | ------ | ---- | ---- | ------- | 396| mouseEvent | [MouseEvent](js-apis-mouseevent.md#mouseevent) | Yes | No| Mouse event data. | 397 398## TouchEventData<sup>11+</sup> 399 400Defines the touchscreen event data. 401 402**System capability**: SystemCapability.MultimodalInput.Input.InputSimulator 403 404| Name | Type | Readable | Writable | Description | 405| --------- | ------ | ---- | ---- | ------- | 406| touchEvent | [TouchEvent](js-apis-touchevent.md#touchevent) | Yes | No| Touchscreen event data | 407