1e41f4b71Sopenharmony_ci# Event Listening Development (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Introduction 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe multimodal module provides applications with the ability to listen for key and input events (mouse, touch, and axis events). Currently, event listening is available only for screen recording applications. For example, when a user starts screen recording, your application can listen for key, mouse, touch, and axis events of the device. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Available APIs 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe following table lists the APIs for event listening. For details, see [Input](../../reference/apis-input-kit/input.md). 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci| API | Description| 12e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | -------------------------- | 13e41f4b71Sopenharmony_ci| Input_Result OH_Input_AddKeyEventMonitor(Input_KeyEventCallback callback) |Creates a key event listener.| 14e41f4b71Sopenharmony_ci| Input_Result OH_Input_AddMouseEventMonitor(Input_MouseEventCallback callback) |Creates a mouse event listener.| 15e41f4b71Sopenharmony_ci| Input_Result OH_Input_AddTouchEventMonitor(Input_TouchEventCallback callback) |Creates a touch event listener.| 16e41f4b71Sopenharmony_ci| Input_Result OH_Input_AddAxisEventMonitorForAll(Input_AxisEventCallback callback) |Creates a listener for all axis events.| 17e41f4b71Sopenharmony_ci| Input_Result OH_Input_AddAxisEventMonitor(InputEvent_AxisEventType axisEventType, Input_AxisEventCallback callback) |Creates a listener for the specified type of axis events.| 18e41f4b71Sopenharmony_ci| Input_Result OH_Input_RemoveKeyEventMonitor(Input_KeyEventCallback callback) |Removes a key event listener.| 19e41f4b71Sopenharmony_ci| Input_Result OH_Input_RemoveMouseEventMonitor(Input_MouseEventCallback callback) |Removes a mouse event listener.| 20e41f4b71Sopenharmony_ci| Input_Result OH_Input_RemoveTouchEventMonitor(Input_TouchEventCallback callback) |Removes a touch event listener.| 21e41f4b71Sopenharmony_ci| Input_Result OH_Input_RemoveAxisEventMonitorForAll(Input_AxisEventCallback callback) |Removes a listener for all axis events.| 22e41f4b71Sopenharmony_ci| Input_Result OH_Input_RemoveAxisEventMonitor(InputEvent_AxisEventType axisEventType, Input_AxisEventCallback callback) |Removes a listener for the specified type of axis events.| 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci## How to Develop 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci### Linking a Dynamic Library 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_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: 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci```txt 31e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libohinput.so) 32e41f4b71Sopenharmony_ci``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci### Applying for Required Permissions 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ciDeclare the required permission in the **module.json5** file. For details, see [Declaring Permissions in the Configuration File](../../security/AccessToken/declare-permissions.md). 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci```json 39e41f4b71Sopenharmony_ci"requestPermissions": [ 40e41f4b71Sopenharmony_ci { 41e41f4b71Sopenharmony_ci "name": "ohos.permission.INTERCEPT_INPUT_EVENT" 42e41f4b71Sopenharmony_ci } 43e41f4b71Sopenharmony_ci] 44e41f4b71Sopenharmony_ci``` 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci### Creating an Event Listener 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci#### Key Events 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci```c++ 51e41f4b71Sopenharmony_ci#include "multimodalinput/oh_input_manager.h" 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_cistruct KeyEvent { 54e41f4b71Sopenharmony_ci int32_t action; 55e41f4b71Sopenharmony_ci int32_t keyCode; 56e41f4b71Sopenharmony_ci int64_t actionTime { -1 }; 57e41f4b71Sopenharmony_ci}; 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci// Define the key event callback function. 60e41f4b71Sopenharmony_civoid OnKeyEventCallback(const Input_KeyEvent* keyEvent) 61e41f4b71Sopenharmony_ci{ 62e41f4b71Sopenharmony_ci KeyEvent event; 63e41f4b71Sopenharmony_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. 64e41f4b71Sopenharmony_ci event.action = OH_Input_GetKeyEventAction(keyEvent); 65e41f4b71Sopenharmony_ci event.keyCode = OH_Input_GetKeyEventKeyCode(keyEvent); 66e41f4b71Sopenharmony_ci event.actionTime = OH_Input_GetKeyEventActionTime(keyEvent); 67e41f4b71Sopenharmony_ci} 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_civoid TestMonitor() 70e41f4b71Sopenharmony_ci{ 71e41f4b71Sopenharmony_ci // Add a key event listener. 72e41f4b71Sopenharmony_ci Input_Result ret = OH_Input_AddKeyEventMonitor(OnKeyEventCallback); 73e41f4b71Sopenharmony_ci // Remove the key event listener. 74e41f4b71Sopenharmony_ci ret = OH_Input_RemoveKeyEventMonitor(OnKeyEventCallback); 75e41f4b71Sopenharmony_ci} 76e41f4b71Sopenharmony_ci``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci#### Mouse Events 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci```c++ 81e41f4b71Sopenharmony_ci#include "multimodalinput/oh_input_manager.h" 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_cistruct MouseEvent { 84e41f4b71Sopenharmony_ci int32_t action; 85e41f4b71Sopenharmony_ci int32_t displayX; 86e41f4b71Sopenharmony_ci int32_t displayY; 87e41f4b71Sopenharmony_ci int32_t button { -1 }; 88e41f4b71Sopenharmony_ci int32_t axisType { -1 }; 89e41f4b71Sopenharmony_ci float axisValue { 0.0f }; 90e41f4b71Sopenharmony_ci int64_t actionTime { -1 }; 91e41f4b71Sopenharmony_ci}; 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci// Define the mouse event callback function. 94e41f4b71Sopenharmony_civoid OnMouseEventCallback(const Input_MouseEvent* mouseEvent) 95e41f4b71Sopenharmony_ci{ 96e41f4b71Sopenharmony_ci MouseEvent event; 97e41f4b71Sopenharmony_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. 98e41f4b71Sopenharmony_ci event.action = OH_Input_GetMouseEventAction(mouseEvent); 99e41f4b71Sopenharmony_ci event.displayX = OH_Input_GetMouseEventDisplayX(mouseEvent); 100e41f4b71Sopenharmony_ci event.displayY = OH_Input_GetMouseEventDisplayY(mouseEvent); 101e41f4b71Sopenharmony_ci event.button = OH_Input_GetMouseEventButton(mouseEvent); 102e41f4b71Sopenharmony_ci event.axisType = OH_Input_GetMouseEventAxisType(mouseEvent); 103e41f4b71Sopenharmony_ci event.axisValue = OH_Input_GetMouseEventAxisValue(mouseEvent); 104e41f4b71Sopenharmony_ci event.actionTime = OH_Input_GetMouseEventActionTime(mouseEvent); 105e41f4b71Sopenharmony_ci} 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_civoid TestMonitor() 108e41f4b71Sopenharmony_ci{ 109e41f4b71Sopenharmony_ci // Add a mouse event listener. 110e41f4b71Sopenharmony_ci Input_Result ret = OH_Input_AddMouseEventMonitor(OnMouseEventCallback); 111e41f4b71Sopenharmony_ci // Remove the mouse event listener. 112e41f4b71Sopenharmony_ci ret = OH_Input_RemoveMouseEventMonitor(OnMouseEventCallback); 113e41f4b71Sopenharmony_ci} 114e41f4b71Sopenharmony_ci``` 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci#### Touch Events 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci```c++ 119e41f4b71Sopenharmony_ci#include "multimodalinput/oh_input_manager.h" 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_cistruct TouchEvent { 122e41f4b71Sopenharmony_ci int32_t action; 123e41f4b71Sopenharmony_ci int32_t id; 124e41f4b71Sopenharmony_ci int32_t displayX; 125e41f4b71Sopenharmony_ci int32_t displayY; 126e41f4b71Sopenharmony_ci int64_t actionTime { -1 }; 127e41f4b71Sopenharmony_ci}; 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci// Define the touch event callback function. 130e41f4b71Sopenharmony_civoid OnTouchEventCallback(const Input_TouchEvent* touchEvent) 131e41f4b71Sopenharmony_ci{ 132e41f4b71Sopenharmony_ci TouchEvent event; 133e41f4b71Sopenharmony_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. 134e41f4b71Sopenharmony_ci event.action = OH_Input_GetTouchEventAction(touchEvent); 135e41f4b71Sopenharmony_ci event.id = OH_Input_GetTouchEventFingerId(touchEvent); 136e41f4b71Sopenharmony_ci event.displayX = OH_Input_GetTouchEventDisplayX(touchEvent); 137e41f4b71Sopenharmony_ci event.displayY = OH_Input_GetTouchEventDisplayY(touchEvent); 138e41f4b71Sopenharmony_ci event.actionTime = OH_Input_GetTouchEventActionTime(touchEvent); 139e41f4b71Sopenharmony_ci} 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_civoid TestMonitor() 142e41f4b71Sopenharmony_ci{ 143e41f4b71Sopenharmony_ci // Add a touch event listener. 144e41f4b71Sopenharmony_ci Input_Result ret = OH_Input_AddTouchEventMonitor(OnTouchEventCallback); 145e41f4b71Sopenharmony_ci // Remove the touch event listener. 146e41f4b71Sopenharmony_ci ret = OH_Input_RemoveTouchEventMonitor(OnTouchEventCallback); 147e41f4b71Sopenharmony_ci} 148e41f4b71Sopenharmony_ci``` 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci#### Axis Events 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci```c++ 153e41f4b71Sopenharmony_ci#include "multimodalinput/oh_input_manager.h" 154e41f4b71Sopenharmony_ci#include <map> 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_cistruct AxisEvent { 157e41f4b71Sopenharmony_ci int32_t axisAction; 158e41f4b71Sopenharmony_ci float displayX; 159e41f4b71Sopenharmony_ci float displayY; 160e41f4b71Sopenharmony_ci std::map<int32_t, double> axisValues; 161e41f4b71Sopenharmony_ci int64_t actionTime { -1 }; 162e41f4b71Sopenharmony_ci int32_t sourceType; 163e41f4b71Sopenharmony_ci int32_t axisEventType { -1 }; 164e41f4b71Sopenharmony_ci}; 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci// Define the callback function for all axis events. 167e41f4b71Sopenharmony_civoid OnAllAxisEventCallback(const Input_AxisEvent* axisEvent) 168e41f4b71Sopenharmony_ci{ 169e41f4b71Sopenharmony_ci AxisEvent event; 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_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. 172e41f4b71Sopenharmony_ci InputEvent_AxisAction action; 173e41f4b71Sopenharmony_ci Input_Result ret = OH_Input_GetAxisEventAction(axisEvent, &action); 174e41f4b71Sopenharmony_ci event.axisAction = action; 175e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventDisplayX(axisEvent, &event.displayX); 176e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventDisplayY(axisEvent, &event.displayY); 177e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventActionTime(axisEvent, &event.actionTime); 178e41f4b71Sopenharmony_ci InputEvent_SourceType sourceType; 179e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventSourceType(axisEvent, &sourceType); 180e41f4b71Sopenharmony_ci event.sourceType = sourceType; 181e41f4b71Sopenharmony_ci InputEvent_AxisEventType axisEventType; 182e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventType(axisEvent, &axisEventType); 183e41f4b71Sopenharmony_ci event.axisEventType = axisEventType; 184e41f4b71Sopenharmony_ci if (event.axisEventType == AXIS_EVENT_TYPE_PINCH) { 185e41f4b71Sopenharmony_ci double value = 0; 186e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_PINCH, &value); 187e41f4b71Sopenharmony_ci event.axisValues.insert(std::make_pair(AXIS_TYPE_PINCH, value)); 188e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_ROTATE, &value); 189e41f4b71Sopenharmony_ci event.axisValues.insert(std::make_pair(AXIS_TYPE_ROTATE, value)); 190e41f4b71Sopenharmony_ci } else if (event.axisEventType == AXIS_EVENT_TYPE_SCROLL) { 191e41f4b71Sopenharmony_ci double value = 0; 192e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_SCROLL_VERTICAL, &value); 193e41f4b71Sopenharmony_ci event.axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_VERTICAL, value)); 194e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_SCROLL_HORIZONTAL, &value); 195e41f4b71Sopenharmony_ci event.axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_HORIZONTAL, value)); 196e41f4b71Sopenharmony_ci } 197e41f4b71Sopenharmony_ci} 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci// Define the callback function for pinch-type axis events. 200e41f4b71Sopenharmony_civoid OnPinchAxisEventCallback(const Input_AxisEvent* axisEvent) 201e41f4b71Sopenharmony_ci{ 202e41f4b71Sopenharmony_ci AxisEvent event; 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_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. 205e41f4b71Sopenharmony_ci InputEvent_AxisAction action; 206e41f4b71Sopenharmony_ci Input_Result ret = OH_Input_GetAxisEventAction(axisEvent, &action); 207e41f4b71Sopenharmony_ci event.axisAction = action; 208e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventDisplayX(axisEvent, &event.displayX); 209e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventDisplayY(axisEvent, &event.displayY); 210e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventActionTime(axisEvent, &event.actionTime); 211e41f4b71Sopenharmony_ci InputEvent_SourceType sourceType; 212e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventSourceType(axisEvent, &sourceType); 213e41f4b71Sopenharmony_ci event.sourceType = sourceType; 214e41f4b71Sopenharmony_ci InputEvent_AxisEventType axisEventType; 215e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventType(axisEvent, &axisEventType); 216e41f4b71Sopenharmony_ci event.axisEventType = axisEventType; 217e41f4b71Sopenharmony_ci double value = 0; 218e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_PINCH, &value); 219e41f4b71Sopenharmony_ci event.axisValues.insert(std::make_pair(AXIS_TYPE_PINCH, value)); 220e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_ROTATE, &value); 221e41f4b71Sopenharmony_ci event.axisValues.insert(std::make_pair(AXIS_TYPE_ROTATE, value)); 222e41f4b71Sopenharmony_ci} 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci// Define the callback function for scroll wheel-type axis events. 225e41f4b71Sopenharmony_civoid OnScrollAxisEventCallback(const Input_AxisEvent* axisEvent) 226e41f4b71Sopenharmony_ci{ 227e41f4b71Sopenharmony_ci AxisEvent event; 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_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. 230e41f4b71Sopenharmony_ci InputEvent_AxisAction action; 231e41f4b71Sopenharmony_ci Input_Result ret = OH_Input_GetAxisEventAction(axisEvent, &action); 232e41f4b71Sopenharmony_ci event.axisAction = action; 233e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventDisplayX(axisEvent, &event.displayX); 234e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventDisplayY(axisEvent, &event.displayY); 235e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventActionTime(axisEvent, &event.actionTime); 236e41f4b71Sopenharmony_ci InputEvent_SourceType sourceType; 237e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventSourceType(axisEvent, &sourceType); 238e41f4b71Sopenharmony_ci event.sourceType = sourceType; 239e41f4b71Sopenharmony_ci InputEvent_AxisEventType axisEventType; 240e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventType(axisEvent, &axisEventType); 241e41f4b71Sopenharmony_ci event.axisEventType = axisEventType; 242e41f4b71Sopenharmony_ci double value = 0; 243e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_SCROLL_VERTICAL, &value); 244e41f4b71Sopenharmony_ci event.axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_VERTICAL, value)); 245e41f4b71Sopenharmony_ci ret = OH_Input_GetAxisEventAxisValue(axisEvent, AXIS_TYPE_SCROLL_HORIZONTAL, &value); 246e41f4b71Sopenharmony_ci event.axisValues.insert(std::make_pair(AXIS_TYPE_SCROLL_HORIZONTAL, value)); 247e41f4b71Sopenharmony_ci} 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_civoid TestMonitor() 250e41f4b71Sopenharmony_ci{ 251e41f4b71Sopenharmony_ci // Add a listener for all axis events. 252e41f4b71Sopenharmony_ci Input_Result ret = OH_Input_AddAxisEventMonitorForAll(OnAllAxisEventCallback); 253e41f4b71Sopenharmony_ci // Remove the listener for all axis events. 254e41f4b71Sopenharmony_ci ret = OH_Input_RemoveAxisEventMonitorForAll(OnAllAxisEventCallback); 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci // Add a listener for pinch-type axis events. 257e41f4b71Sopenharmony_ci ret = OH_Input_AddAxisEventMonitor(AXIS_EVENT_TYPE_PINCH, OnPinchAxisEventCallback); 258e41f4b71Sopenharmony_ci // Remove the listener for pinch-type axis events. 259e41f4b71Sopenharmony_ci ret = OH_Input_RemoveAxisEventMonitor(AXIS_EVENT_TYPE_PINCH, OnPinchAxisEventCallback); 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci // Add a listener for scroll wheel-type axis events. 262e41f4b71Sopenharmony_ci ret = OH_Input_AddAxisEventMonitor(AXIS_EVENT_TYPE_SCROLL, OnScrollAxisEventCallback); 263e41f4b71Sopenharmony_ci // Remove the listener for scroll wheel-type axis events. 264e41f4b71Sopenharmony_ci ret = OH_Input_RemoveAxisEventMonitor(AXIS_EVENT_TYPE_SCROLL, OnScrollAxisEventCallback); 265e41f4b71Sopenharmony_ci} 266e41f4b71Sopenharmony_ci``` 267