1# Using NativeWindowEventFilter to Filter Multimodal Input Events (C/C++) 2 3## When to Use 4 5You can use the capability provided by the NativeWindowEventFilter module to intercept key events so that they are not distributed to internal components of your application. 6 7 8## Available APIs 9 10| API | Description | 11| -------- | -------- | 12| OH_NativeWindowManager_RegisterKeyEventFilter (int32_t windowId, OH_NativeWindowManager_KeyEventFilter keyEventFilter); | Registers a key event filter for a window. | 13| OH_NativeWindowManager_UnregisterKeyEventFilter(int32_t windowId)| Unregisters the key event filter of a window. | 14 15## How to Develop 16### Linking the Dynamic Library in the CMake Script 17``` 18target_link_libraries(entry PUBLIC libnative_window_manager.so libohinput.so) 19``` 20 21### Adding Header Files 22``` 23#include "window_manager/oh_window_comm.h" 24#include "window_manager/oh_window_event_filter.h" 25#include "multimodalinput/oh_input_manager.h" 26#include "multimodalinput/oh_key_code.h" 27``` 28 29### API Usage Description 30- After an application window is created, bind a key event filter to the window specified by the window ID. 31- The application window can trigger the key event interception only when it receives the key event. 32- The application window intercepts the event when the return value of the key event filter is **true**. If the return value is **false**, it does not intercept the event. 33- Only one key event filter can be registered for the same window ID. The last registered key event filter overwrites the previously registered one. To filter a multi-key event, you are advised to process the multi-key combination in a filter. 34 35## Example 36The 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. 37``` 38#include "napi/native_api.h" 39#include "window_manager/oh_window_comm.h" 40#include "window_manager/oh_window_event_filter.h" 41#include "multimodalinput/oh_input_manager.h" 42#include "multimodalinput/oh_key_code.h" 43 44// Set a filter. 45static bool filterFunc(Input_KeyEvent *event) { 46 auto keyCode = OH_Input_GetKeyEventKeyCode(event); 47 auto action = OH_Input_GetKeyEventAction(event); 48 // Case1: Implement the event filter for the Esc key. 49 // return keyCode == Input_KeyCode::KEYCODE_ESCAPE; 50 51 // Case 2: Implement the event filter for the number keys only when they are pressed. 52 // return keyCode >= Input_KeyCode::KEYCODE_0 && keyCode <= Input_KeyCode::KEYCODE_9 53 // && action == Input_KeyEventAction::KEY_ACTION_DOWN; 54 55 // Implement the event filter for the combination of the Esc key and a pressed-down number key. (Case1 || Case2). 56 return (keyCode >= Input_KeyCode::KEYCODE_0 && keyCode <= Input_KeyCode::KEYCODE_9 57 && action == Input_KeyEventAction::KEY_ACTION_DOWN) || (keyCode == Input_KeyCode::KEYCODE_ESCAPE); 58} 59 60static napi_value registerFilter(napi_env env, napi_callback_info info) { 61 size_t argc = 1; 62 napi_value args[1] = {nullptr}; 63 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 64 65 int32_t windowId; 66 napi_get_value_int32(env, args[0], &windowId); 67 68 // Register the filter for the window specified by the window ID. 69 auto res = OH_NativeWindowManager_RegisterKeyEventFilter(windowId, filterFunc); 70 71 napi_value errCode; 72 napi_create_int32(env, res, &errCode); 73 return errCode; 74} 75 76static napi_value clearFilter(napi_env env, napi_callback_info info) { 77 size_t argc = 1; 78 napi_value args[1] = {nullptr}; 79 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 80 81 int32_t windowId; 82 napi_get_value_int32(env, args[0], &windowId); 83 84 auto res = OH_NativeWindowManager_UnregisterKeyEventFilter(windowId); 85 napi_value errCode; 86 napi_create_int32(env, res, &errCode); 87 return errCode; 88 89} 90 91EXTERN_C_START 92static napi_value Init(napi_env env, napi_value exports) { 93 napi_property_descriptor desc[] = { 94 {"registerFilter", nullptr, registerFilter, nullptr, nullptr, nullptr, napi_default, nullptr}, 95 {"clearFilter", nullptr, clearFilter, nullptr, nullptr, nullptr, napi_default, nullptr}}; 96 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 97 return exports; 98} 99EXTERN_C_END 100 101``` 102