1e41f4b71Sopenharmony_ci# Event Interception Development (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Introduction
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThe multimodal module provides applications with the ability to create and delete keystrokes and intercept input events. For example, you can intercept key, mouse, touch, and axis events.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Available APIs
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciThe following table lists the APIs for event interception. For details, see [Input](../../reference/apis-input-kit/input.md).
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci| API | Description|
12e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | -------------------------- |
13e41f4b71Sopenharmony_ci| Input_Result OH_Input_AddKeyEventInterceptor(Input_KeyEventCallback callback, Input_InterceptorOptions *option) |Creates a key event interceptor.|
14e41f4b71Sopenharmony_ci| Input_Result OH_Input_AddInputEventInterceptor(Input_InterceptorEventCallback *callback, Input_InterceptorOptions *option) |Creates an input event interceptor. Input events include mouse, touch, and axis events.|
15e41f4b71Sopenharmony_ci| Input_Result OH_Input_RemoveKeyEventInterceptor() |Removes a key event interceptor.|
16e41f4b71Sopenharmony_ci| Input_Result OH_Input_RemoveInputEventInterceptor() |Removes an input event interceptor.|
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## How to Develop
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci### Linking a Dynamic Library
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ciBefore calling interception-related APIs, you need to link the related dynamic library. You can do this by editing the **CMakeList.txt** file as follows:
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci```txt
25e41f4b71Sopenharmony_ci   target_link_libraries(entry PUBLIC libohinput.so)
26e41f4b71Sopenharmony_ci```
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci### Applying for Required Permissions
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ciDeclare the required permission in the **module.json5** file. For details, see [Declaring Permissions in the Configuration File](../../security/AccessToken/declare-permissions.md).
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci```json
33e41f4b71Sopenharmony_ci"requestPermissions": [
34e41f4b71Sopenharmony_ci    {
35e41f4b71Sopenharmony_ci        "name": "ohos.permission.INTERCEPT_INPUT_EVENT"
36e41f4b71Sopenharmony_ci    }
37e41f4b71Sopenharmony_ci]
38e41f4b71Sopenharmony_ci```
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci### Creating an Event Interceptor
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci#### Key Events
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci```c++
45e41f4b71Sopenharmony_ci#include "multimodalinput/oh_input_manager.h"
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_cistruct KeyEvent {
48e41f4b71Sopenharmony_ci    int32_t action;
49e41f4b71Sopenharmony_ci    int32_t keyCode;
50e41f4b71Sopenharmony_ci    int64_t actionTime { -1 };
51e41f4b71Sopenharmony_ci};
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci// Define the key event callback function.
54e41f4b71Sopenharmony_civoid OnKeyEventCallback(const Input_KeyEvent* keyEvent)
55e41f4b71Sopenharmony_ci{
56e41f4b71Sopenharmony_ci    KeyEvent event;
57e41f4b71Sopenharmony_ci    // The lifecycle of Input_KeyEvent is limited to the callback function. Input_KeyEvent will be destroyed if it is called outside of the callback function.
58e41f4b71Sopenharmony_ci    event.action = OH_Input_GetKeyEventAction(keyEvent);
59e41f4b71Sopenharmony_ci    event.keyCode = OH_Input_GetKeyEventKeyCode(keyEvent);
60e41f4b71Sopenharmony_ci    event.actionTime = OH_Input_GetKeyEventActionTime(keyEvent);
61e41f4b71Sopenharmony_ci}
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_civoid TestInterceptor()
64e41f4b71Sopenharmony_ci{
65e41f4b71Sopenharmony_ci    // Add the key event interceptor.
66e41f4b71Sopenharmony_ci    Input_Result ret = OH_Input_AddKeyEventInterceptor(OnKeyEventCallback, nullptr);
67e41f4b71Sopenharmony_ci    // Remove the key event interceptor.
68e41f4b71Sopenharmony_ci    ret = OH_Input_RemoveKeyEventInterceptor();
69e41f4b71Sopenharmony_ci}
70e41f4b71Sopenharmony_ci```
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci#### Input Events
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci```c++
75e41f4b71Sopenharmony_ci#include "multimodalinput/oh_input_manager.h"
76e41f4b71Sopenharmony_ci#include <map>
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_cistruct MouseEvent {
79e41f4b71Sopenharmony_ci    int32_t action;
80e41f4b71Sopenharmony_ci    int32_t displayX;
81e41f4b71Sopenharmony_ci    int32_t displayY;
82e41f4b71Sopenharmony_ci    int32_t button { -1 };
83e41f4b71Sopenharmony_ci    int32_t axisType { -1 };
84e41f4b71Sopenharmony_ci    float axisValue { 0.0f };
85e41f4b71Sopenharmony_ci    int64_t actionTime { -1 };
86e41f4b71Sopenharmony_ci};
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_cistruct TouchEvent {
89e41f4b71Sopenharmony_ci    int32_t action;
90e41f4b71Sopenharmony_ci    int32_t id;
91e41f4b71Sopenharmony_ci    int32_t displayX;
92e41f4b71Sopenharmony_ci    int32_t displayY;
93e41f4b71Sopenharmony_ci    int64_t actionTime { -1 };
94e41f4b71Sopenharmony_ci};
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_cistruct AxisEvent {
97e41f4b71Sopenharmony_ci    int32_t axisAction;
98e41f4b71Sopenharmony_ci    float displayX;
99e41f4b71Sopenharmony_ci    float displayY;
100e41f4b71Sopenharmony_ci    std::map<int32_t, double> axisValues;
101e41f4b71Sopenharmony_ci    int64_t actionTime { -1 };
102e41f4b71Sopenharmony_ci    int32_t sourceType;
103e41f4b71Sopenharmony_ci    int32_t axisEventType { -1 };
104e41f4b71Sopenharmony_ci};
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci// Define the mouse event callback function.
107e41f4b71Sopenharmony_civoid OnMouseEventCallback(const Input_MouseEvent* mouseEvent)
108e41f4b71Sopenharmony_ci{
109e41f4b71Sopenharmony_ci    MouseEvent event;
110e41f4b71Sopenharmony_ci    // The lifecycle of Input_MouseEvent is limited to the callback function. Input_MouseEvent will be destroyed if it is called outside of the callback function.
111e41f4b71Sopenharmony_ci    event.action = OH_Input_GetMouseEventAction(mouseEvent);
112e41f4b71Sopenharmony_ci    event.displayX = OH_Input_GetMouseEventDisplayX(mouseEvent);
113e41f4b71Sopenharmony_ci    event.displayY = OH_Input_GetMouseEventDisplayY(mouseEvent);
114e41f4b71Sopenharmony_ci    event.button = OH_Input_GetMouseEventButton(mouseEvent);
115e41f4b71Sopenharmony_ci    event.axisType = OH_Input_GetMouseEventAxisType(mouseEvent);
116e41f4b71Sopenharmony_ci    event.axisValue = OH_Input_GetMouseEventAxisValue(mouseEvent);
117e41f4b71Sopenharmony_ci    event.actionTime = OH_Input_GetMouseEventActionTime(mouseEvent);
118e41f4b71Sopenharmony_ci}
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci// Define the touch event callback function.
121e41f4b71Sopenharmony_civoid OnTouchEventCallback(const Input_TouchEvent* touchEvent)
122e41f4b71Sopenharmony_ci{
123e41f4b71Sopenharmony_ci    TouchEvent event;
124e41f4b71Sopenharmony_ci    // The lifecycle of Input_TouchEvent is limited to the callback function. Input_TouchEvent will be destroyed if it is called outside of the callback function.
125e41f4b71Sopenharmony_ci    event.action = OH_Input_GetTouchEventAction(touchEvent);
126e41f4b71Sopenharmony_ci    event.id = OH_Input_GetTouchEventFingerId(touchEvent);
127e41f4b71Sopenharmony_ci    event.displayX = OH_Input_GetTouchEventDisplayX(touchEvent);
128e41f4b71Sopenharmony_ci    event.displayY = OH_Input_GetTouchEventDisplayY(touchEvent);
129e41f4b71Sopenharmony_ci    event.actionTime = OH_Input_GetTouchEventActionTime(touchEvent);
130e41f4b71Sopenharmony_ci}
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci// Define the axis event callback function.
133e41f4b71Sopenharmony_civoid OnAxisEventCallback(const Input_AxisEvent* axisEvent)
134e41f4b71Sopenharmony_ci{
135e41f4b71Sopenharmony_ci    AxisEvent event;
136e41f4b71Sopenharmony_ci    
137e41f4b71Sopenharmony_ci    // The lifecycle of Input_AxisEvent is limited to the callback function. Input_AxisEvent will be destroyed if it is called outside of the callback function.
138e41f4b71Sopenharmony_ci    InputEvent_AxisAction action;
139e41f4b71Sopenharmony_ci    Input_Result ret = OH_Input_GetAxisEventAction(axisEvent, &action);
140e41f4b71Sopenharmony_ci    event.axisAction = action;
141e41f4b71Sopenharmony_ci    ret = OH_Input_GetAxisEventDisplayX(axisEvent, &event.displayX);
142e41f4b71Sopenharmony_ci    ret = OH_Input_GetAxisEventDisplayY(axisEvent, &event.displayY);
143e41f4b71Sopenharmony_ci    ret = OH_Input_GetAxisEventActionTime(axisEvent, &event.actionTime);
144e41f4b71Sopenharmony_ci    InputEvent_SourceType sourceType;
145e41f4b71Sopenharmony_ci    ret = OH_Input_GetAxisEventSourceType(axisEvent, &sourceType);
146e41f4b71Sopenharmony_ci    event.sourceType = sourceType;
147e41f4b71Sopenharmony_ci    InputEvent_AxisEventType axisEventType;
148e41f4b71Sopenharmony_ci    ret = OH_Input_GetAxisEventType(axisEvent, &axisEventType);
149e41f4b71Sopenharmony_ci    event.axisEventType = axisEventType;
150e41f4b71Sopenharmony_ci    if (event.axisEventType == AXIS_EVENT_TYPE_PINCH) {
151e41f4b71Sopenharmony_ci        double value = 0;
152e41f4b71Sopenharmony_ci        ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_PINCH, &value);
153e41f4b71Sopenharmony_ci        event.axisValues.insert(std::make_pair(AXIS_TYPE_PINCH, value));
154e41f4b71Sopenharmony_ci        ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_ROTATE, &value);
155e41f4b71Sopenharmony_ci        event.axisValues.insert(std::make_pair(AXIS_TYPE_ROTATE, value));
156e41f4b71Sopenharmony_ci    } else if (event.axisEventType == AXIS_EVENT_TYPE_SCROLL) {
157e41f4b71Sopenharmony_ci        double value = 0;
158e41f4b71Sopenharmony_ci        ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_SCROLL_VERTICAL, &value);
159e41f4b71Sopenharmony_ci        event.axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_VERTICAL, value));
160e41f4b71Sopenharmony_ci        ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_SCROLL_HORIZONTAL, &value);
161e41f4b71Sopenharmony_ci        event.axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_HORIZONTAL, value));
162e41f4b71Sopenharmony_ci    }
163e41f4b71Sopenharmony_ci}
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci// Structure of the input event callback function
166e41f4b71Sopenharmony_ciInput_InterceptorEventCallback g_eventCallback;
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_civoid TestInterceptor()
169e41f4b71Sopenharmony_ci{
170e41f4b71Sopenharmony_ci    // Set the mouse event callback function.
171e41f4b71Sopenharmony_ci    g_eventCallback.mouseCallback = OnMouseEventCallback;
172e41f4b71Sopenharmony_ci    // Set the touch event callback function.
173e41f4b71Sopenharmony_ci    g_eventCallback.touchCallback = OnTouchEventCallback;
174e41f4b71Sopenharmony_ci    // Set the axis event callback function.
175e41f4b71Sopenharmony_ci    g_eventCallback.axisCallback = OnAxisEventCallback;
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci    // Add the input event interceptor.
178e41f4b71Sopenharmony_ci    Input_Result ret = OH_Input_AddInputEventInterceptor(&g_eventCallback, nullptr);
179e41f4b71Sopenharmony_ci    // Remove the input event interceptor.
180e41f4b71Sopenharmony_ci    ret = OH_Input_RemoveInputEventInterceptor();
181e41f4b71Sopenharmony_ci}
182e41f4b71Sopenharmony_ci```
183