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 77## inputEventClient.injectKeyEvent<sup>11+</sup> 78 79injectKeyEvent(keyEvent: KeyEventData): void 80 81按键(包括单个按键和组合键)事件注入。 82 83**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 84 85**需要权限:** ohos.permission.INJECT_INPUT_EVENT 86 87**参数:** 88 89| 参数名 | 类型 | 必填 | 说明 | 90| -------- | --------------------- | ---- | --------- | 91| keyEvent | [KeyEventData](#keyeventdata11) | 是 | 按键事件注入描述信息。 | 92 93**错误码**: 94 95以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 96 97| 错误码ID | 错误信息 | 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**示例:** 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 141鼠标/触摸板事件注入。 142 143**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 144 145**需要权限:** ohos.permission.INJECT_INPUT_EVENT 146 147**参数:** 148 149| 参数名 | 类型 | 必填 | 说明 | 150| -------- | --------------------- | ---- | --------- | 151| mouseEvent | [MouseEventData](#mouseeventdata11) | 是 | 鼠标/触摸板事件注入描述信息。 | 152 153**错误码**: 154 155以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 156 157| 错误码ID | 错误信息 | 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**示例:** 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 243触摸屏事件注入。 244 245**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 246 247**需要权限:** ohos.permission.INJECT_INPUT_EVENT 248 249**参数:** 250 251| 参数名 | 类型 | 必填 | 说明 | 252| -------- | --------------------- | ---- | --------- | 253| touchEvent | [TouchEventData](#toucheventdata11) | 是 | 触摸屏事件注入描述信息。 | 254 255**错误码**: 256 257以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 258 259| 错误码ID | 错误信息 | 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**示例:** 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 333允许事件注入权限。 334 335**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 336 337**需要权限:** ohos.permission.INJECT_INPUT_EVENT 338 339**参数:** 340 341| 参数名 | 类型 | 必填 | 说明 | 342| -------- | ------ | ---- | --------- | 343| result | boolean | 是 | 授权结果(true表示:允许事件注入,false表示:不允许事件注入)。 | 344 345**错误码**: 346 347以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 348 349| 错误码ID | 错误信息 | 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 368按键注入描述信息。 369 370**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 371 372| 名称 | 类型 | 可读 | 可写 | 说明 | 373| --------- | ------ | ---- | ---- | ------- | 374| isPressed | boolean | 是 | 否 | 按键是否按下。<br>true表示按键按下,false表示按键抬起。 | 375| keyCode | number | 是 | 否 | 按键键码值。当前仅支持返回键/KEYCODE_BACK键。 | 376| keyDownDuration | number | 是 | 否 | 按键按下持续时间,单位为微秒(μs)。 | 377| isIntercepted | boolean | 是 | 否 | 按键是否可以被拦截。<br>true表示可以被拦截,false表示不可被拦截。 | 378 379## KeyEventData<sup>11+</sup> 380 381按键注入描述信息。 382 383**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 384 385| 名称 | 类型 | 必填 | 说明 | 386| --------- | ------ | ---- | ------- | 387| keyEvent | [KeyEvent](#keyevent) | 是 | 按键注入描述信息。 | 388 389## MouseEventData<sup>11+</sup> 390 391鼠标注入描述信息。 392 393**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 394 395| 名称 | 类型 | 可读 | 可写 | 说明 | 396| --------- | ------ | ---- | ---- | ------- | 397| mouseEvent | [MouseEvent](js-apis-mouseevent.md#mouseevent) | 是 | 否 | 鼠标注入描述信息。 | 398 399## TouchEventData<sup>11+</sup> 400 401触摸屏注入描述信息。 402 403**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 404 405| 名称 | 类型 | 可读 | 可写 | 说明 | 406| --------- | ------ | ---- | ---- | ------- | 407| touchEvent | [TouchEvent](js-apis-touchevent.md#touchevent) | 是 | 否 | 触摸屏注入描述信息。 |