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