# @ohos.multimodalInput.inputEventClient (输入事件注入)(系统接å£) 输入事件注入模å—,æä¾›è¾“入事件注入能力。 > **说明:** > > - 本模å—首批接å£ä»ŽAPI version 8开始支æŒã€‚åŽç»ç‰ˆæœ¬çš„æ–°å¢žæŽ¥å£ï¼Œé‡‡ç”¨ä¸Šè§’æ ‡å•ç‹¬æ ‡è®°æŽ¥å£çš„起始版本。 > > - æœ¬æ¨¡å—æŽ¥å£ä¸ºç³»ç»ŸæŽ¥å£ã€‚ ## å¯¼å…¥æ¨¡å— ```js import { inputEventClient } from '@kit.InputKit'; ``` ## inputEventClient.injectEvent injectEvent({KeyEvent: KeyEvent}): void 按键(包括å•个按键和组åˆé”®)注入。 **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator **éœ€è¦æƒé™ï¼š** ohos.permission.INJECT_INPUT_EVENT **傿•°ï¼š** | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------- | --------------------- | ---- | --------- | | KeyEvent | [KeyEvent](#keyevent) | 是 | 按键注入æè¿°ä¿¡æ¯ã€‚ | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ---- | --------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```js try { let backKeyDown: inputEventClient.KeyEvent = { isPressed: true, keyCode: 2, keyDownDuration: 0, isIntercepted: false } class EventDown { KeyEvent: inputEventClient.KeyEvent | null = null } let eventDown: EventDown = { KeyEvent: backKeyDown } inputEventClient.injectEvent(eventDown); let backKeyUp: inputEventClient.KeyEvent = { isPressed: false, keyCode: 2, keyDownDuration: 0, isIntercepted: false }; class EventUp { KeyEvent: inputEventClient.KeyEvent | null = null } let eventUp: EventUp = { KeyEvent: backKeyUp } inputEventClient.injectEvent(eventUp); } catch (error) { console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); } ``` ## inputEventClient.injectKeyEvent<sup>11+</sup> injectKeyEvent(keyEvent: KeyEventData): void 按键(包括å•个按键和组åˆé”®)事件注入。 **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator **éœ€è¦æƒé™ï¼š** ohos.permission.INJECT_INPUT_EVENT **傿•°ï¼š** | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------- | --------------------- | ---- | --------- | | keyEvent | [KeyEventData](#keyeventdata11) | 是 | 按键事件注入æè¿°ä¿¡æ¯ã€‚ | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ---- | --------------------- | | 202 | SystemAPI permission error. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```js try { let backKeyDown: inputEventClient.KeyEvent = { isPressed: true, keyCode: 2, keyDownDuration: 0, isIntercepted: false } class EventDown { KeyEvent: inputEventClient.KeyEvent | null = null } let eventDown: EventDown = { KeyEvent: backKeyDown } inputEventClient.injectKeyEvent(eventDown); let backKeyUp: inputEventClient.KeyEvent = { isPressed: false, keyCode: 2, keyDownDuration: 0, isIntercepted: false }; class EventUp { KeyEvent: inputEventClient.KeyEvent | null = null } let eventUp: EventUp = { KeyEvent: backKeyUp } inputEventClient.injectKeyEvent(eventUp); } catch (error) { console.log(`Failed to inject KeyEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); } ``` ## inputEventClient.injectMouseEvent<sup>11+</sup> injectMouseEvent(mouseEvent: MouseEventData): void; é¼ æ ‡/触摸æ¿äº‹ä»¶æ³¨å…¥ã€‚ **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator **éœ€è¦æƒé™ï¼š** ohos.permission.INJECT_INPUT_EVENT **傿•°ï¼š** | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------- | --------------------- | ---- | --------- | | mouseEvent | [MouseEventData](#mouseeventdata11) | 是 | é¼ æ ‡/触摸æ¿äº‹ä»¶æ³¨å…¥æè¿°ä¿¡æ¯ã€‚ | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ---- | --------------------- | | 202 | SystemAPI permission error. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```js import { MouseEvent } from '@kit.InputKit'; try { let mouseButtonUpData: mouseEvent.MouseEvent = { id: 0, deviceId: 1, actionTime: 2, screenId: 1, windowId: 0, action: 3, screenX: 100, screenY: 200, windowX: 100, windowY: 200, rawDeltaX: 200, rawDeltaY: 200, button: 2, pressedButtons: [2], axes: [], pressedKeys: [0], ctrlKey: false, altKey: false, shiftKey: false, logoKey: false, fnKey: false, capsLock: false, numLock: false, scrollLock: false, toolType: 1, } let mouseButtonUp: inputEventClient.MouseEventData = { mouseEvent: mouseButtonUpData } inputEventClient.injectMouseEvent(mouseButtonUp); let mouseButtonDownData: mouseEvent.MouseEvent = { id: 0, deviceId: 1, actionTime: 2, screenId: 1, windowId: 0, action: 2, screenX: 100, screenY: 200, windowX: 100, windowY: 200, rawDeltaX: 200, rawDeltaY: 200, button: 2, pressedButtons: [2], axes: [], pressedKeys: [0], ctrlKey: false, altKey: false, shiftKey: false, logoKey: false, fnKey: false, capsLock: false, numLock: false, scrollLock: false, toolType: 1, } let mouseButtonDown: inputEventClient.MouseEventData = { mouseEvent: mouseButtonDownData }; inputEventClient.injectMouseEvent(mouseButtonDown); } catch (error) { console.log(`Failed to inject MouseEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); } ``` ## inputEventClient.injectTouchEvent<sup>11+</sup> injectTouchEvent(touchEvent: TouchEventData): void 触摸å±äº‹ä»¶æ³¨å…¥ã€‚ **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator **éœ€è¦æƒé™ï¼š** ohos.permission.INJECT_INPUT_EVENT **傿•°ï¼š** | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------- | --------------------- | ---- | --------- | | touchEvent | [TouchEventData](#toucheventdata11) | 是 | 触摸å±äº‹ä»¶æ³¨å…¥æè¿°ä¿¡æ¯ã€‚ | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ---- | --------------------- | | 202 | SystemAPI permission error. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```js import { TouchEvent } from '@kit.InputKit'; try { let touchEvent: touchEvent.Touch = { id: 1, pressedTime: 1, screenX: 0, screenY: 0, windowX: 0, windowY: 0, pressure: 0, width: 0, height: 0, tiltX: 0, tiltY: 0, toolX: 0, toolY: 0, toolWidth: 0, toolHeight: 0, rawX: 0, rawY: 0, toolType: 0, } let touchEventUpData: touchEvent.TouchEvent = { action: 1, sourceType: 0, touch: touchEvent, touches: [], id: 0, deviceId: 0, actionTime: 0, screenId: 0, windowId: 0 } ; let touchEventUp: inputEventClient.TouchEventData = { touchEvent: touchEventUpData } inputEventClient.injectTouchEvent(touchEventUp); let touchEventDownData: touchEvent.TouchEvent = { action: 1, sourceType: 0, touch: touchEvent, touches: [], id: 0, deviceId: 0, actionTime: 0, screenId: 0, windowId: 0 } ; let touchEventDown: inputEventClient.TouchEventData = { touchEvent: touchEventDownData } inputEventClient.injectTouchEvent(touchEventDown); } catch (error) { console.log(`Failed to inject touchEvent, error: ${JSON.stringify(error, [`code`, `message`])}`); } ``` ## inputEventClient.permitInjection<sup>12+</sup> permitInjection(result: boolean): void å…许事件注入æƒé™ã€‚ **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator **éœ€è¦æƒé™ï¼š** ohos.permission.INJECT_INPUT_EVENT **傿•°ï¼š** | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------- | ------ | ---- | --------- | | result | boolean | 是 | 授æƒç»“果(true表示:å…许事件注入,false表示:ä¸å…许事件注入)。 | **错误ç **: 以下错误ç 的详细介ç»è¯·å‚è§[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ---- | --------------------- | | 202 | SystemAPI permission error. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed. | ```ts import { inputEventClient } from '@kit.InputKit'; try { let result = true; inputEventClient.permitInjection(result); }catch(error){ console.error("failed:" + JSON.stringify(error)); } ``` ## KeyEvent 按键注入æè¿°ä¿¡æ¯ã€‚ **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator | åç§° | 类型 | å¯è¯» | å¯å†™ | 说明 | | --------- | ------ | ---- | ---- | ------- | | isPressed | boolean | 是 | å¦ | æŒ‰é”®æ˜¯å¦æŒ‰ä¸‹ã€‚<br>true表示按键按下,false表示按键抬起。 | | keyCode | number | 是 | å¦ | 按键键ç 值。当å‰ä»…支æŒè¿”回键/KEYCODE_BACK键。 | | keyDownDuration | number | 是 | å¦ | 按键按下æŒç»æ—¶é—´ï¼Œå•ä½ä¸ºå¾®ç§’(μs)。 | | isIntercepted | boolean | 是 | å¦ | 按键是å¦å¯ä»¥è¢«æ‹¦æˆªã€‚<br>true表示å¯ä»¥è¢«æ‹¦æˆªï¼Œfalse表示ä¸å¯è¢«æ‹¦æˆªã€‚ | ## KeyEventData<sup>11+</sup> 按键注入æè¿°ä¿¡æ¯ã€‚ **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator | åç§° | 类型 | å¿…å¡« | 说明 | | --------- | ------ | ---- | ------- | | keyEvent | [KeyEvent](#keyevent) | 是 | 按键注入æè¿°ä¿¡æ¯ã€‚ | ## MouseEventData<sup>11+</sup> é¼ æ ‡æ³¨å…¥æè¿°ä¿¡æ¯ã€‚ **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator | åç§° | 类型 | å¯è¯» | å¯å†™ | 说明 | | --------- | ------ | ---- | ---- | ------- | | mouseEvent | [MouseEvent](js-apis-mouseevent.md#mouseevent) | 是 | å¦ | é¼ æ ‡æ³¨å…¥æè¿°ä¿¡æ¯ã€‚ | ## TouchEventData<sup>11+</sup> è§¦æ‘¸å±æ³¨å…¥æè¿°ä¿¡æ¯ã€‚ **系统能力:** SystemCapability.MultimodalInput.Input.InputSimulator | åç§° | 类型 | å¯è¯» | å¯å†™ | 说明 | | --------- | ------ | ---- | ---- | ------- | | touchEvent | [TouchEvent](js-apis-touchevent.md#touchevent) | 是 | å¦ | è§¦æ‘¸å±æ³¨å…¥æè¿°ä¿¡æ¯ã€‚ |