Writing to HID keyboard works on Linux but not on Windows

945 views Asked by At

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 error, 'Invalid function', which I'm assuming is equivalent to the system Error 0x1, ERROR_INVALID_FUNCTION.

This sample example piece of code will not run on Windows, but will run on Linux. (When it is run from the terminal with 'sudo' prepended to the command.)

// Device is found prior to this...

if (device != null) {
    device.disableBlocking();

    // Initialise the buffer, and send it. PACKET_SIZE is 264
    byte[] buffer = new byte[PACKET_SIZE];
    for (int i = 0; i < PACKET_SIZE; i++) { buffer[i] = (byte)0x00; }

    // These bytes are required for it to actually change the LED's.
    buffer[0] = 0x07;
    buffer[1] = 0x0E;
    buffer[2] = 0x01;
    buffer[3] = 0x01;

    try {
        // Actually send the data.
        device.sendFeatureReport(buffer);
    }
    // Handle I/O exceptions
    catch (IOException e) {
        e.printStackTrace(); 
    }

    // Close the device.
    device.close();
}
else {
    System.err.println("DEVICE IS NULL");
}

I've tried running as administrator, no difference unfortunately...

If my question is not clear enough, let me know and I'll attempt to clarify it further.

Any help would be greatly appreciated! I'm getting desperate to find a solution to this! :)


Versions of OS's, if it matters:
Linux Ubuntu 18.04
Windows 8.1

1

There are 1 answers

2
Michael On BEST ANSWER

Alright, so I managed to finally get it working! Basically, I switched from the Java HIDAPI to hid4java, modified the code to do the same thing, and it worked like a charm ! (On Windows)

I also removed the first byte in the buffer, and used it as the reportId parameter in hid4java's HidDevice.sendFeatureReport function. This shifted all the bytes over by 1, but this was easily fixed by subtracting 1 at the index when a value in the buffer was to be set. (Not shown in the example of my question.)

I haven't yet tested this on Linux though, but I'd assume it would still work.