目录

Using WindowManager to Manage Multimodal Input Events (C/C++)

When to Use

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.

Filtering Multimodal Input Events

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.

[h2]Linking the Dynamic Library in the CMake Script

target_link_libraries(entry PUBLIC libnative_window_manager.so libohinput.so)

[h2]Available APIs

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.

[h2]Example

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

Injecting Multimodal Touch Events

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.

[h2]Linking the Dynamic Library in the CMake Script

target_link_libraries(entry PUBLIC libnative_window_manager.so libohinput.so)

[h2]Available APIs

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.

[h2]Example

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