Skip to main content

OH_Input_InjectKeyEvent

Function OH_Input_InjectKeyEvent 

Source
pub unsafe extern "C" fn OH_Input_InjectKeyEvent(
    keyEvent: *const Input_KeyEvent,
) -> i32
Expand description

@brief Injects a key event.
This API does not take effect if the user has not granted authorization and the caller does not have the ohos. permission.CONTROL_DEVICE permission.
Since API version 20, you are advised to use {@link OH_Input_RequestInjection()} to request the required permission before calling this API. If the status returned by {@link OH_Input_QueryAuthorizedStatus()} is {@link AUTHORIZED}, then you can call this API.
Since API version 22, if the key press event (KEY_ACTION_DOWN) of a modifier key (KEYCODE_META_LEFT, ** KEYCODE_META_RIGHT**, KEYCODE_CTRL_LEFT, KEYCODE_CTRL_RIGHT, KEYCODE_ALT_LEFT, KEYCODE_ALT_RIGHT, ** KEYCODE_SHIFT_LEFT**, KEYCODE_SHIFT_RIGHT, KEYCODE_CAPS_LOCK, KEYCODE_SCROLL_LOCK, or *KEYCODE_NUM_LOCK *) is injected, the release event (KEY_ACTION_UP) of the key needs to be injected in a timely manner to avoid the key being pressed for a long time.
Since API version 26.0.0, callers that have the ohos.permission.CONTROL_DEVICE permission can use this API directly.

@permission ohos.permission.CONTROL_DEVICE @param keyEvent KeyEvent object, which can be created through {@link OH_Input_CreateKeyEvent()}. You can call {@link OH_Input_SetKeyEventKeyCode()} and {@link OH_Input_SetKeyEventAction()} to set the key value and key event type of the key event object.
If the key event object is no longer needed, destroy it by calling {@link OH_Input_DestroyKeyEvent()}. @return Return value of the OH_Input_InjectKeyEvent function.
- {@link INPUT_SUCCESS} if the operation is successful;
- {@link INPUT_PERMISSION_DENIED} if the required permission is missing;
- {@link INPUT_PARAMETER_ERROR} if the input parameter is incorrect. @since 12