1# @ohos.multimodalInput.inputEventClient (输入事件注入)(系统接口) 2 3<!--Kit: Input Kit--> 4<!--Subsystem: MultimodalInput--> 5<!--Owner: @zhaoxueyuan--> 6<!--Designer: @hanruofei--> 7<!--Tester: @Lyuxin--> 8<!--Adviser: @Brilliantry_Rui--> 9 10输入事件注入模块,提供输入事件注入能力。 11 12> **说明:** 13> 14> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> 16> - 本模块接口为系统接口。 17 18## 导入模块 19 20```js 21import { inputEventClient } from '@kit.InputKit'; 22``` 23 24## inputEventClient.injectEvent 25 26injectEvent({KeyEvent: KeyEvent}): void 27 28按键(包括单个按键和组合键)注入。 29 30**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 31 32**需要权限:** ohos.permission.INJECT_INPUT_EVENT 33 34**参数:** 35 36| 参数名 | 类型 | 必填 | 说明 | 37| -------- | --------------------- | ---- | --------- | 38| KeyEvent | [KeyEvent](#keyevent) | 是 | 按键注入描述信息。 | 39 40**错误码**: 41 42以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 43 44| 错误码ID | 错误信息 | 45| ---- | --------------------- | 46| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 47 48**示例:** 49 50```js 51import { inputEventClient } from '@kit.InputKit'; 52 53@Entry 54@Component 55struct Index { 56 build() { 57 RelativeContainer() { 58 Text() 59 .onClick(() => { 60 try { 61 let backKeyDown: inputEventClient.KeyEvent = { 62 isPressed: true, 63 keyCode: 2, 64 keyDownDuration: 0, 65 isIntercepted: false 66 } 67 68 class EventDown { 69 KeyEvent: inputEventClient.KeyEvent | null = null 70 } 71 72 let eventDown: EventDown = { KeyEvent: backKeyDown } 73 inputEventClient.injectEvent(eventDown); 74 75 let backKeyUp: inputEventClient.KeyEvent = { 76 isPressed: false, 77 keyCode: 2, 78 keyDownDuration: 0, 79 isIntercepted: false 80 }; 81 82 class EventUp { 83 KeyEvent: inputEventClient.KeyEvent | null = null 84 } 85 86 let eventUp: EventUp = { KeyEvent: backKeyUp } 87 inputEventClient.injectEvent(eventUp); 88 } catch (error) { 89 console.error(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); 90 } 91 }) 92 } 93 } 94} 95``` 96 97## inputEventClient.injectKeyEvent<sup>11+</sup> 98 99injectKeyEvent(keyEvent: KeyEventData): void 100 101按键(包括单个按键和组合键)事件注入。 102 103**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 104 105**需要权限:** ohos.permission.INJECT_INPUT_EVENT 106 107**参数:** 108 109| 参数名 | 类型 | 必填 | 说明 | 110| -------- | --------------------- | ---- | --------- | 111| keyEvent | [KeyEventData](#keyeventdata11) | 是 | 按键事件注入描述信息。 | 112 113**错误码**: 114 115以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 116 117| 错误码ID | 错误信息 | 118| ---- | --------------------- | 119| 202 | SystemAPI permission error. | 120| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 121 122**示例:** 123 124```js 125import { inputEventClient } from '@kit.InputKit'; 126 127@Entry 128@Component 129struct Index { 130 build() { 131 RelativeContainer() { 132 Text() 133 .onClick(() => { 134 try { 135 let backKeyDown: inputEventClient.KeyEvent = { 136 isPressed: true, 137 keyCode: 2, 138 keyDownDuration: 0, 139 isIntercepted: false 140 } 141 142 class EventDown { 143 keyEvent: inputEventClient.KeyEvent | null = null 144 } 145 146 let eventDown: EventDown = { keyEvent: backKeyDown } 147 inputEventClient.injectKeyEvent(eventDown); 148 149 let backKeyUp: inputEventClient.KeyEvent = { 150 isPressed: false, 151 keyCode: 2, 152 keyDownDuration: 0, 153 isIntercepted: false 154 }; 155 156 class EventUp { 157 keyEvent: inputEventClient.KeyEvent | null = null 158 } 159 160 let eventUp: EventUp = { keyEvent: backKeyUp } 161 inputEventClient.injectKeyEvent(eventUp); 162 } catch (error) { 163 console.error(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); 164 } 165 }) 166 } 167 } 168} 169``` 170## inputEventClient.injectMouseEvent<sup>11+</sup> 171 172injectMouseEvent(mouseEvent: MouseEventData): void 173 174鼠标/触摸板事件注入。 175 176**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 177 178**需要权限:** ohos.permission.INJECT_INPUT_EVENT 179 180**参数:** 181 182| 参数名 | 类型 | 必填 | 说明 | 183| -------- | --------------------- | ---- | --------- | 184| mouseEvent | [MouseEventData](#mouseeventdata11) | 是 | 鼠标/触摸板事件注入描述信息。 | 185 186**错误码**: 187 188以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 189 190| 错误码ID | 错误信息 | 191| ---- | --------------------- | 192| 202 | SystemAPI permission error. | 193| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 194 195**示例:** 196 197```js 198import { inputEventClient } from '@kit.InputKit'; 199import { MouseEvent } from '@kit.InputKit'; 200 201@Entry 202@Component 203struct Index { 204 build() { 205 RelativeContainer() { 206 Text() 207 .onClick(() => { 208 try { 209 let mouseButtonUpData: MouseEvent = { 210 id: 0, 211 deviceId: 1, 212 actionTime: 2, 213 screenId: 1, 214 windowId: 0, 215 action: 3, 216 screenX: 100, 217 screenY: 200, 218 windowX: 100, 219 windowY: 200, 220 rawDeltaX: 200, 221 rawDeltaY: 200, 222 button: 2, 223 pressedButtons: [2], 224 axes: [], 225 pressedKeys: [0], 226 ctrlKey: false, 227 altKey: false, 228 shiftKey: false, 229 logoKey: false, 230 fnKey: false, 231 capsLock: false, 232 numLock: false, 233 scrollLock: false, 234 toolType: 1, 235 } 236 let mouseButtonUp: inputEventClient.MouseEventData = { 237 mouseEvent: mouseButtonUpData 238 } 239 inputEventClient.injectMouseEvent(mouseButtonUp); 240 241 let mouseButtonDownData: MouseEvent = { 242 id: 0, 243 deviceId: 1, 244 actionTime: 2, 245 screenId: 1, 246 windowId: 0, 247 action: 2, 248 screenX: 100, 249 screenY: 200, 250 windowX: 100, 251 windowY: 200, 252 rawDeltaX: 200, 253 rawDeltaY: 200, 254 button: 2, 255 pressedButtons: [2], 256 axes: [], 257 pressedKeys: [0], 258 ctrlKey: false, 259 altKey: false, 260 shiftKey: false, 261 logoKey: false, 262 fnKey: false, 263 capsLock: false, 264 numLock: false, 265 scrollLock: false, 266 toolType: 1, 267 } 268 let mouseButtonDown: inputEventClient.MouseEventData = { 269 mouseEvent: mouseButtonDownData 270 }; 271 inputEventClient.injectMouseEvent(mouseButtonDown); 272 } 273 274 catch (error) { 275 console.error(`Failed to inject MouseEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); 276 } 277 }) 278 } 279 } 280} 281``` 282 283## inputEventClient.injectTouchEvent<sup>11+</sup> 284 285injectTouchEvent(touchEvent: TouchEventData): void 286 287触摸屏事件注入。 288 289**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 290 291**需要权限:** ohos.permission.INJECT_INPUT_EVENT 292 293**参数:** 294 295| 参数名 | 类型 | 必填 | 说明 | 296| -------- | --------------------- | ---- | --------- | 297| touchEvent | [TouchEventData](#toucheventdata11) | 是 | 触摸屏事件注入描述信息。 | 298 299**错误码**: 300 301以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 302 303| 错误码ID | 错误信息 | 304| ---- | --------------------- | 305| 202 | SystemAPI permission error. | 306| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 307 308**示例:** 309 310```js 311import { inputEventClient } from '@kit.InputKit'; 312import { Touch, TouchEvent } from '@kit.InputKit'; 313 314@Entry 315@Component 316struct Index { 317 build() { 318 RelativeContainer() { 319 Text() 320 .onClick(() => { 321 try { 322 let touchEvent: Touch = { 323 id: 1, 324 pressedTime: 1, 325 screenX: 0, 326 screenY: 0, 327 windowX: 0, 328 windowY: 0, 329 pressure: 0, 330 width: 0, 331 height: 0, 332 tiltX: 0, 333 tiltY: 0, 334 toolX: 0, 335 toolY: 0, 336 toolWidth: 0, 337 toolHeight: 0, 338 rawX: 0, 339 rawY: 0, 340 toolType: 0, 341 } 342 343 let touchEventUpData: TouchEvent = { 344 action: 1, 345 sourceType: 0, 346 touch: touchEvent, 347 touches: [], 348 id: 0, 349 deviceId: 0, 350 actionTime: 0, 351 screenId: 0, 352 windowId: 0 353 } 354 ; 355 let touchEventUp: inputEventClient.TouchEventData = { 356 touchEvent: touchEventUpData 357 } 358 inputEventClient.injectTouchEvent(touchEventUp); 359 360 let touchEventDownData: TouchEvent = { 361 action: 1, 362 sourceType: 0, 363 touch: touchEvent, 364 touches: [], 365 id: 0, 366 deviceId: 0, 367 actionTime: 0, 368 screenId: 0, 369 windowId: 0 370 } 371 ; 372 let touchEventDown: inputEventClient.TouchEventData = { 373 touchEvent: touchEventDownData 374 } 375 inputEventClient.injectTouchEvent(touchEventDown); 376 } catch (error) { 377 console.error(`Failed to inject touchEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); 378 } 379 }) 380 } 381 } 382} 383``` 384 385## inputEventClient.permitInjection<sup>12+</sup> 386 387permitInjection(result: boolean): void 388 389允许事件注入权限。 390 391**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 392 393**需要权限:** ohos.permission.INJECT_INPUT_EVENT 394 395**参数:** 396 397| 参数名 | 类型 | 必填 | 说明 | 398| -------- | ------ | ---- | --------- | 399| result | boolean | 是 | 授权结果(true表示:允许事件注入,false表示:不允许事件注入)。 | 400 401**错误码**: 402 403以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 404 405| 错误码ID | 错误信息 | 406| ---- | --------------------- | 407| 202 | SystemAPI permission error. | 408| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | 409 410 411```ts 412import { inputEventClient } from '@kit.InputKit'; 413 414@Entry 415@Component 416struct Index { 417 build() { 418 RelativeContainer() { 419 Text() 420 .onClick(() => { 421 try { 422 let result = true; 423 inputEventClient.permitInjection(result); 424 }catch(error){ 425 console.error("failed:" + JSON.stringify(error)); 426 } 427 }) 428 } 429 } 430} 431``` 432 433## KeyEvent 434 435按键注入描述信息。 436 437**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 438 439| 名称 | 类型 | 只读 | 可选 | 说明 | 440| --------- | ------ | ---- | ---- | ------- | 441| isPressed | boolean | 否 | 否 | 按键是否按下。<br>true表示按键按下,false表示按键抬起。 | 442| keyCode | number | 否 | 否 | 按键键码值。当前仅支持返回键/KEYCODE_BACK键。 | 443| keyDownDuration | number | 否 | 否 | 按键按下持续时间,单位为微秒(μs)。 | 444| isIntercepted | boolean | 否 | 否 | 按键是否可以被拦截。<br>true表示可以被拦截,false表示不可被拦截。 | 445 446## KeyEventData<sup>11+</sup> 447 448按键注入描述信息。 449 450**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 451 452| 名称 | 类型 | 必填 | 说明 | 453| --------- | ------ | ---- | ------- | 454| keyEvent | [KeyEvent](#keyevent) | 是 | 按键注入描述信息。 | 455 456## MouseEventData<sup>11+</sup> 457 458鼠标注入描述信息。 459 460**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 461 462| 名称 | 类型 | 只读 | 可选 | 说明 | 463| --------- | ------ | ---- | ---- | ------- | 464| mouseEvent | [MouseEvent](js-apis-mouseevent.md#mouseevent) | 否 | 否 | 鼠标注入描述信息。 | 465| useGlobalCoordinate<sup>20+</sup> | boolean | 否 | 是 | 是否使用全局坐标来计算注入的鼠标事件。默认值为false,取值为false表示使用以指定屏幕左上角为原点的相对坐标系的坐标来计算注入的鼠标事件。取值为true表示使用以主屏左上角为原点的全局坐标系的坐标来计算注入的鼠标事件。 | 466 467## TouchEventData<sup>11+</sup> 468 469触摸屏注入描述信息。 470 471**系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator 472 473| 名称 | 类型 | 只读 | 可选 | 说明 | 474| --------- | ------ | ---- | ---- | ------- | 475| touchEvent | [TouchEvent](js-apis-touchevent.md#touchevent) | 否 | 否 | 触摸屏注入描述信息。 | 476| useGlobalCoordinate<sup>20+</sup> | boolean | 否 | 是 | 是否使用全局坐标来计算注入的触摸屏事件。默认值为false,取值为false表示使用以指定屏幕左上角为原点的相对坐标系的坐标来计算注入的触摸屏事件。取值为true表示使用以主屏左上角为原点的全局坐标系的坐标来计算注入的触摸屏事件。 |