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.error(`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.error(`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.error(`Failed to inject MouseEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); 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 { Touch, TouchEvent } from '@kit.InputKit'; 267 268try { 269 let 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 = { 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 = { 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.error(`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