1# HID DDK Development 2 3## When to Use 4 5The HID driver development kit (DDK) is a toolset that helps you develop HID device drivers at the application layer based on the user mode. It provides interfaces for accessing devices from a host, including creating a device, sending events to a device, and destroying a device. 6 7## Available APIs 8 9| Name| Description| 10| -------- | -------- | 11| OH_Hid_CreateDevice(Hid_Device *hidDevice, Hid_EventProperties *hidEventProperties) | Creates a HID device. When the device is no longer needed, use **OH_Hid_DestroyDevice** to destroy it.| 12| OH_Hid_EmitEvent(int32_t deviceId, const Hid_EmitItem items[], uint16_t length) | Sends events to a HID device.| 13| OH_Hid_DestroyDevice(int32_t deviceId) | Destroys a HID device.| 14 15For details about the APIs, see [HID DDK](../reference/apis-driverdevelopment-kit/_hid_ddk.md). 16 17## How to Develop 18 19The following steps you through the development of a HID device driver with the HID DDK. 20 21**Adding the Dynamic Link Library** 22 23Add the following library to **CMakeLists.txt**. 24```txt 25libhid.z.so 26``` 27 28**Including Header Files** 29```c++ 30#include <hid/hid_ddk_api.h> 31#include <hid/hid_ddk_types.h> 32``` 33 341. Create a device.<br>Use **OH_Hid_CreateDevice** in **hid_ddk_api.h** to create a HID device. If the operation is successful, **deviceId** (a non-negative number) is returned. If the operation fails, an error code (a negative number) is returned. 35 36 ```c++ 37 // Construct HID device properties. 38 std::vector<Hid_DeviceProp> deviceProp = {HID_PROP_DIRECT}; 39 std::string deviceName = "keyboard" 40 Hid_Device hidDevice = { 41 .deviceName = deviceName.c_str(), 42 .vendorId = 0x6006, 43 .productId = 0x6006, 44 .version = 1, 45 .bustype = 3, 46 .properties = deviceProp.data(), 47 .propLength = (uint16_t)deviceProp.size() 48 }; 49 // Construct the event properties related to the HID device. 50 std::vector<Hid_EventType> eventType = {HID_EV_ABS, HID_EV_KEY, HID_EV_SYN, HID_EV_MSC}; 51 Hid_EventTypeArray eventTypeArray = {.hidEventType = eventType.data(), .length = (uint16_t)eventType.size()}; 52 std::vector<Hid_KeyCode> keyCode = {HID_BTN_TOOL_PEN, HID_BTN_TOOL_RUBBER, HID_BTN_TOUCH, HID_BTN_STYLUS, HID_BTN_RIGHT}; 53 Hid_KeyCodeArray keyCodeArray = {.hidKeyCode = keyCode.data(), .length = (uint16_t)keyCode.size()}; 54 std::vector<Hid_MscEvent> mscEvent = {HID_MSC_SCAN}; 55 Hid_MscEventArray mscEventArray = {.hidMscEvent = mscEvent.data(), .length = (uint16_t)mscEvent.size()}; 56 std::vector<Hid_AbsAxes> absAxes = {HID_ABS_X, HID_ABS_Y, HID_ABS_PRESSURE}; 57 Hid_AbsAxesArray absAxesArray = {.hidAbsAxes = absAxes.data(), .length = (uint16_t)absAxes.size()}; 58 Hid_EventProperties hidEventProp = { 59 .hidEventTypes = eventTypeArray, 60 .hidKeys = keyCodeArray, 61 .hidAbs = absAxesArray, 62 .hidMiscellaneous = mscEventArray 63 }; 64 // Create a device. The device ID of the device created is returned. 65 int32_t deviceId = OH_Hid_CreateDevice(&hidDevice, &hidEventProp); 66 ``` 67 682. Send an event to a HID device.<br>Use **OH_Hid_EmitEvent** of **hid_ddk_api.h** to send an event to the device with the specified **deviceId**. 69 70 ```c++ 71 // Construct the event to be sent. 72 Hid_EmitItem event = {.type = HID_EV_MSC, .code = HID_MSC_SCAN, .value = 0x000d0042}; 73 std::vector<Hid_EmitItem> itemVec; 74 itemVec.push_back(event); 75 // Send the event to a HID device. 76 int32_t ret = OH_Hid_EmitEvent(deviceId, itemVec.data(), (uint16_t)itemVec.size()); 77 ``` 78 793. Release resources.<br>Use **OH_Hid_DestroyDevice** of **hid_ddk_api.h** to destroy the device after all requests are processed and before the application exits. 80 81 ```c++ 82 // Destroy a HID device. 83 int32_t ret = OH_Hid_DestroyDevice(deviceId); 84 ``` 85