WindowManager provides the capability to manage application windows and can be used to handle multimodal input events.
Currently, WindowManager supports filtering multimodal input events and injecting multimodal touch events into target windows. The specific development steps are detailed below.
You can use the capability provided by the WindowManager module to intercept key events so that they are not distributed to internal components of your application.
target_link_libraries(entry PUBLIC libnative_window_manager.so libohinput.so)
| API | Description |
|---|---|
| OH_NativeWindowManager_RegisterKeyEventFilter(int32_t windowId, OH_NativeWindowManager_KeyEventFilter keyEventFilter) | Registers a key event filter for a window. |
| OH_NativeWindowManager_UnregisterKeyEventFilter(int32_t windowId) | Unregisters the key event filter of a window. |
The following sample code describes how to register and unregister a key event filter. The following uses the Esc key and number keys as an example.
#include "napi/native_api.h"
#include "window_manager/oh_window_comm.h"
#include "window_manager/oh_window_event_filter.h"
#include "multimodalinput/oh_input_manager.h"
#include "multimodalinput/oh_key_code.h"
// Set a filter.
static bool filterFunc(Input_KeyEvent *event)
{
auto keyCode = OH_Input_GetKeyEventKeyCode(event);
auto action = OH_Input_GetKeyEventAction(event);
// Filter the press of the Escape key and number keys.
return (keyCode >= Input_KeyCode::KEYCODE_0 && keyCode <= Input_KeyCode::KEYCODE_9
&& action == Input_KeyEventAction::KEY_ACTION_DOWN) || (keyCode == Input_KeyCode::KEYCODE_ESCAPE);
}
static napi_value registerFilter(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
int32_t windowId;
napi_get_value_int32(env, args[0], &windowId);
// Register the filter function filterFunc for the window corresponding to windowId.
auto res = OH_NativeWindowManager_RegisterKeyEventFilter(windowId, filterFunc);
napi_value errCode;
napi_create_int32(env, res, &errCode);
return errCode;
}
static napi_value clearFilter(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
int32_t windowId;
napi_get_value_int32(env, args[0], &windowId);
auto res = OH_NativeWindowManager_UnregisterKeyEventFilter(windowId);
napi_value errCode;
napi_create_int32(env, res, &errCode);
return errCode;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{"registerFilter", nullptr, registerFilter, nullptr, nullptr, nullptr, napi_default, nullptr},
{"clearFilter", nullptr, clearFilter, nullptr, nullptr, nullptr, napi_default, nullptr}};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END You can use the capability provided by the WindowManager module to inject touch events into a specified window. This operation is supported only for windows within the same process. It does not trigger changes in window focus, z-order, or dragging. Instead, the events are directly sent to ArkUI.
target_link_libraries(entry PUBLIC libnative_window_manager.so libohinput.so)
| API | Description |
|---|---|
| OH_WindowManager_InjectTouchEvent(int32_t windowId, Input_TouchEvent* touchEvent, int32_t windowX, int32_t windowY) | Injects a touch event into the specified window. |
Construct event parameters and inject the event to the window with the specified window ID.
Only windows within the same process are supported. Injection does not trigger changes in window focus, z-order, or dragging. Events are directly sent to ArkUI.
The API must be called after the specified window has completed its UI loading.
Check the window and multimodal touch event to ensure the event parameters are correct before sending the event to ArkUI. The specific parameter descriptions are as follows:
| Name | Description |
|---|---|
| windowId | ID of the target window. Only windows within the same process are supported. Otherwise, error code 1300002 is returned. The window must have completed UI loading. Otherwise, error code 1300003 is returned. |
| touchEvent | Pointer to the multimodal touch event. For details, see Input_TouchEvent. The event is defined in oh_input_manager.h. Call OH_Input_CreateTouchEvent to create a touchEvent object, and call OH_Input_DestroyTouchEvent to destroy the object after use. For specific parameter details, see the table below. |
| windowX | X coordinate of the event relative to the target window. The parameter must be a non-negative integer; otherwise, error code 1300003 is returned. |
| windowY | Y coordinate of the event relative to the target window. The parameter must be a non-negative integer; otherwise, error code 1300003 is returned. |
The following table describes the parameters of the touchEvent event.
| Name | API | Description |
|---|---|---|
| action | OH_Input_SetTouchEventAction | Event action. The default value is 0. Only the following actions are supported: - 0: cancel, indicating a cancel event. - 1: down, indicating a press-down event. - 2: move, indicating a move event. - 3: up, indicating a lift event. - For other actions, error code 1300003 is returned. |
| id | OH_Input_SetTouchEventFingerId | Finger ID. The default value is 0. The parameter must be a non-negative integer; otherwise, error code 1300003 is returned. |
| displayX | OH_Input_SetTouchEventDisplayX | X coordinate of the event relative to the screen. The default value is 0. The parameter must be a non-negative integer; otherwise, error code 1300003 is returned. It is recommended that the value of this parameter be the same as that of windowX, although inconsistency will not return an error code. Only the input parameter's range is verified. You are advised to use getWindowProperties() to obtain the windowRect property and calculate the corresponding windowX by subtracting the horizontal coordinate of the window's top-left corner in windowRect from displayX. |
| displayY | OH_Input_SetTouchEventDisplayY | Y coordinate of the event relative to the screen. The default value is 0. The parameter must be a non-negative integer; otherwise, error code 1300003 is returned. It is recommended that the value of this parameter be the same as that of windowY, although inconsistency will not return an error code. Only the input parameter's range is verified. You are advised to use getWindowProperties() to obtain the windowRect property and calculate the corresponding windowY by subtracting the vertical coordinate of the window's top-left corner in windowRect from displayY. |
| actionTime | OH_Input_SetTouchEventActionTime | Timestamp. The default value is -1. The parameter must be a non-negative integer; otherwise, error code 1300003 is returned. |
| windowId | OH_Input_SetTouchEventWindowId | Window ID for event injection. The default value is -1. If the parameter is not the default value and does not match windowId in OH_WindowManager_InjectTouchEvent, the input parameter is considered invalid. |
| displayId | OH_Input_SetTouchEventDisplayId | Display ID for event injection. The default value is -1. There are no restrictions, but you are advised to ensure that the display ID matches the window ID in OH_WindowManager_InjectTouchEvent. You are advised to call getWindowProperties() to obtain the displayId property. |
The following sample code describes how to inject a multimodal touch event into the target window, using a single event injection as an example.
#include "napi/native_api.h"
#include "window_manager/oh_window.h"
#include "multimodalinput/oh_input_manager.h"
const int32_t ARGS_TWO = 2;
const int32_t ARGS_THREE = 3;
const int32_t ARGS_FOUR = 4;
const int32_t ARGS_FIVE = 5;
const int32_t ARGS_SIX = 6;
const int32_t ARGS_SEVEN = 7;
const int32_t ARGS_EIGHT = 8;
const int32_t ARGS_NINE = 9;
const int32_t ARGS_TEN = 10;
static napi_value injectEvent(napi_env env, napi_callback_info info)
{
size_t argc = ARGS_TEN;
napi_value args[ARGS_TEN] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
int32_t windowId;
napi_get_value_int32(env, args[0], &windowId);
int32_t displayId;
napi_get_value_int32(env, args[1], &displayId);
int32_t windowX;
napi_get_value_int32(env, args[ARGS_TWO], &windowX);
int32_t windowY;
napi_get_value_int32(env, args[ARGS_THREE], &windowY);
int32_t action;
napi_get_value_int32(env, args[ARGS_FOUR], &action);
int32_t fingerId;
napi_get_value_int32(env, args[ARGS_FIVE], &fingerId);
int32_t displayX;
napi_get_value_int32(env, args[ARGS_SIX], &displayX);
int32_t displayY;
napi_get_value_int32(env, args[ARGS_SEVEN], &displayY);
int32_t actionTime;
napi_get_value_int32(env, args[ARGS_EIGHT], &actionTime);
int32_t TE_WindowId;
napi_get_value_int32(env, args[ARGS_NINE], &TE_WindowId);
// Construct a multimodal event touchEvent.
Input_TouchEvent* touchEvent = OH_Input_CreateTouchEvent();
OH_Input_SetTouchEventAction(touchEvent, action);
OH_Input_SetTouchEventFingerId(touchEvent, fingerId);
OH_Input_SetTouchEventDisplayX(touchEvent, displayX);
OH_Input_SetTouchEventDisplayY(touchEvent, displayY);
OH_Input_SetTouchEventActionTime(touchEvent, actionTime);
OH_Input_SetTouchEventWindowId(touchEvent, TE_WindowId);
OH_Input_SetTouchEventDisplayId(touchEvent, displayId);
// Inject a multimodal touch event into the window corresponding to windowId.
auto res = OH_WindowManager_InjectTouchEvent(windowId, touchEvent, windowX, windowY);
// Destroy the object after using touchEvent.
OH_Input_DestroyTouchEvent(&touchEvent);
napi_value errCode;
napi_create_int32(env, res, &errCode);
return errCode;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{"injectEvent", nullptr, injectEvent, nullptr, nullptr, nullptr, napi_default, nullptr}};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END