Questions tagged [hidapi]

HIDAPI is a multi-platform library which allows an application to interface with USB and Bluetooth HID-Class devices on Windows, Linux, and Mac OS X.

HIDAPI is a multi-platform library which allows an application to interface with USB and Bluetooth HID-Class devices on Windows, Linux, and Mac OS X. While it can be used to communicate with standard HID devices like keyboards, mice, and Joysticks, it is most useful when used with custom (Vendor-Defined) HID devices. Many devices do this in order to not require a custom driver to be written for each platform. HIDAPI is easy to integrate with the client application, just requiring a single source file to be dropped into the application. On Windows, HIDAPI can optionally be built into a DLL.

Programs which use HIDAPI are driverless, meaning they do not require the use of a custom driver for each device on each platform.

HIDAPI provides a clean and consistent interface for each platform, making it easier to develop applications which communicate with USB HID devices without having to know the details of the HID libraries and interfaces on each platform.

The HIDAPI source also provides a GUI test application which can enumerate and communicate with any HID device attached to the system. The test GUI compiles and runs on all platforms supported by HIDAPI.

Example

The sample program, which communicates with a modified version of the USB Generic HID sample which is part of the Microchip Application Library (in folder "Microchip Solutions\USB Device - HID - Custom Demos\Generic HID - Firmware" when the Microchip Application Framework is installed), looks like this (with error checking removed for simplicity):

#include <stdio.h>
#include <stdlib.h>

#include "hidapi.h"


int main(int argc, char* argv[])
{
    int res;
    unsigned char buf[65];
    #define MAX_STR 255
    wchar_t wstr[MAX_STR];
    hid_device *handle;
    int i;

    // Enumerate and print the HID devices on the system
    struct hid_device_info *devs, *cur_dev;

    devs = hid_enumerate(0x0, 0x0);
    cur_dev = devs; 
    while (cur_dev) {
        printf("Device Found\n  type: %04hx %04hx\n  path: %s\n  serial_number: %ls",
            cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
        printf("\n");
        printf("  Manufacturer: %ls\n", cur_dev->manufacturer_string);
        printf("  Product:      %ls\n", cur_dev->product_string);
        printf("\n");
        cur_dev = cur_dev->next;
    }
    hid_free_enumeration(devs);


    // Open the device using the VID, PID,
    // and optionally the Serial number.
    handle = hid_open(0x4d8, 0x3f, NULL);

    // Read the Manufacturer String
    res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
    printf("Manufacturer String: %ls\n", wstr);

    // Read the Product String
    res = hid_get_product_string(handle, wstr, MAX_STR);
    printf("Product String: %ls\n", wstr);

    // Read the Serial Number String
    res = hid_get_serial_number_string(handle, wstr, MAX_STR);
    printf("Serial Number String: %ls", wstr);
    printf("\n");

    // Send a Feature Report to the device
    buf[0] = 0x2; // First byte is report number
    buf[1] = 0xa0;
    buf[2] = 0x0a;
    res = hid_send_feature_report(handle, buf, 17);

    // Read a Feature Report from the device
    buf[0] = 0x2;
    res = hid_get_feature_report(handle, buf, sizeof(buf));

    // Print out the returned buffer.
    printf("Feature Report\n   ");
    for (i = 0; i < res; i++)
        printf("%02hhx ", buf[i]);
    printf("\n");

    // Set the hid_read() function to be non-blocking.
    hid_set_nonblocking(handle, 1);

    // Send an Output report to toggle the LED (cmd 0x80)
    buf[0] = 1; // First byte is report number
    buf[1] = 0x80;
    res = hid_write(handle, buf, 65);

    // Send an Output report to request the state (cmd 0x81)
    buf[1] = 0x81;
    hid_write(handle, buf, 65);

    // Read requested state
    res = hid_read(handle, buf, 65);
    if (res < 0)
        printf("Unable to read()\n");

    // Print out the returned buffer.
    for (i = 0; i < res; i++)
        printf("buf[%d]: %d\n", i, buf[i]);

    return 0;
}

License

HIDAPI may be used by one of three licenses as outlined in LICENSE.txt. These licenses are:

  • GPL v3 (see LICENSE-gpl3.txt),
  • BSD (see LICENSE-bsd.txt),
  • The more liberal original HIDAPI license (see LICENSE-orig.txt).

Download

HIDAPI can be downloaded from GitHub

98 questions
10
votes
2 answers

CreateFileA fails to open HID device in Windows

EDIT: Issue reported here: https://github.com/signal11/hidapi/issues/276 Inkling is a pen-device from Wacom. (InklingReader) is an open source project that gets real-time data from it. I'm trying to tidy up InklingReader to use HIDAPI rather than…
P i
  • 29,020
  • 36
  • 159
  • 267
9
votes
1 answer

hidapi vs libusb for Linux

Writing some C code for USB mouse. More specifically writing configuration and information to mouse like poll, sensitivity, button actions, colors (light), tactile alters, OLED etc. Started out with this old article where libhid is recommended over…
user3342816
  • 974
  • 10
  • 24
6
votes
3 answers

Using HIDAPI, how can you query the raw report descriptor?

I'd like to deconstruct the raw reports received from the hid_read function of hidapi. As I understand, this can be achieved using the information from the device's report descriptors. But when trying to query for those descriptors, I get lost…
Shane Holloway
  • 7,550
  • 4
  • 29
  • 37
5
votes
1 answer

Getting node-hid working in Windows

I'm trying to get the node-hid (https://github.com/node-hid/node-hid) module working on Windows 7. I can open a device and write to it, but no data ever arrives. I've verified the device is sending data. I've dug through the node-hid code (HID.cc)…
Chris
  • 236
  • 3
  • 13
4
votes
2 answers

Interpreting HIDAPI python output

I would like to read a string from a USB HID scanner with Python on OS X. The example below is my starting point and I have been able to tailor the code to my scanner: I have been able to execute the command: h.open() successfully and printout the…
gatorback
  • 1,351
  • 4
  • 19
  • 44
4
votes
1 answer

Read hid mouse/keyboard on windows (hidapi)

I have an application on Linux which use hidapi to read data of a complex device (create by Qt5.3). This device has several interface HID whose one type "mouse" and one type "keyboard". On Linux I have no problem to get all data. I need to deploy…
helene
  • 1,201
  • 2
  • 18
  • 30
3
votes
1 answer

Writing to HID keyboard works on Linux but not on Windows

I am attempting to set the LED's of my RGB keyboard by sending HID packets to it via my Java program, with the Java HIDAPI wrapper found here. I've been successful so far, but only on my Linux laptop. When I try run the code on Windows, I get an…
Michael
  • 115
  • 10
3
votes
1 answer

Using Python hidapi to open device with multiple usages

I'm new to the Python hidapi although I've used the C version that it is based on before. The Python library is really different and I can't figure out how to use it from the one example that is provided. Does anyone know of any good documentation…
ClydeTheGhost
  • 1,473
  • 2
  • 17
  • 31
3
votes
0 answers

Are hid4java set-up and tear-down operations expensive?

I have started prototyping a solution using Gary Rowe's hid4java library with good success. The example provided in that project's test package is a good start, but I have a more general question regarding the proper usage pattern for this…
Sasha Borodin
  • 193
  • 1
  • 1
  • 8
3
votes
1 answer

Applications ignore USB device's incoming data packet while using default HID driver

I am writing a controlling software for a generic USB HID device within a team, working on Windows 7. Due to my status as an intern, my possibilities are limited: the software must work on Windows the software must use the default HID driver…
3
votes
1 answer

How to add the HIDAPI library to an existing Mac project

I'm working on a user-space HID device driver and a lot of googling shows that it is recommended to use the HIDAPI library from signal11. I followed all the readme instructions and ran the make command under the Mac directory but I'm not seeing the…
Stavros_S
  • 2,145
  • 7
  • 31
  • 75
3
votes
0 answers

Linking native hidapi and libusb-1.0 library in eclipse on Ubuntu

I am new to Ubuntu and searched on-line for two days, tried a lot of solutions but do not have any luck yet. If anyone can help with this, I will appreciate. I wrote a program in Java with eclipse on Windows. My program makes use of Java hidapi…
2
votes
2 answers

Send output to a device to change channel in Logitech mouse/keyboard

I have Logitech mouse and keyboard supporting switching between multiple devices. The feature I am missing is to simultaneously switch mouse AND keboard by running a command or a shortcut. I would like to ask if it is possible to send an output to a…
Daniel
  • 145
  • 1
  • 11
2
votes
2 answers

Reverse-engineering a HID handshake by examining bytes over USB

I'm trying to extract real-time data from a Wacom Inkling Roel Janssen has already examined the packets here: // Some kind of handshaking. // Values obtained by sniffing the USB connection between SketchManager and the device. unsigned char…
P i
  • 29,020
  • 36
  • 159
  • 267
2
votes
0 answers

Reading from USB device through HIDAPI on Linux sometimes results in missing data

I am currently porting code that uses a USB device from Windows to Linux. I've thoroughly tested the original application and I'm pretty sure that the device works well. I implemented the USB interface on Linux using hidapi-libusb and there are…
Detox
  • 71
  • 1
  • 5
1
2 3 4 5 6 7