USB Keyboard, mouse and receiving data from PC at same time

I had idea of project using mouse/keyboard and sending data from PC to Spark
Spark can handle only mouse or keyboard by default
Also usb-hid-suport branch is broken and needs reverting few last commits to actually work

So I digged in and finally made it work. Now sharing project code:


void HID_Data_Received_Callback()
    if (HID_Rx_length > 0)
        current_profile = HID_Rx_Buffer[1];

Callback for receiving data from PC. Can receive 8 bytes max, can be reconfigured to 64 bytes max;
    for (uint8_t i = 0; i < count; i++)
        Mouse.move(0, 0, direction);

Using mouse and keyboard in same time

Windows detects this as 3 devices under one USB port

Unfortunately I’m not C++/SMT32 expert. This code could be much better written.
I really hope mouse+keyboad+2way HID communication setup will be available from Spark itself in future
@zachary @Dave :wink:

Source code:

A bit of tech details:
Instead of using mouse OR keyboard HID descriptors I always declare both under same endpoint. But I use 2 different hid report descriptors with different REPORT_ID Both reports can be sent at a same time or one after one(i use this approach)

For sending data back to core (I use it for setting device profiles) I use C# library HidSharp

        HidDeviceLoader loader = new HidDeviceLoader();
        allHidDevices = loader.GetDevices(0x1D50, 0x807D);

        device = allHidDevices.FirstOrDefault(d => d.MaxOutputReportLength == 9);

        if (!device.TryOpen(out stream))

        using (stream)
                var bytes = new byte[device.MaxOutputReportLength];
                bytes[0] = 3; //REPORT ID 3
                bytes[1] = profile;
                catch (TimeoutException)
                catch (Exception ex)

@ryotsuke, that’s cool :+1:

I’ll go and try this out soon.
I’ve recently extended my IoT-Mouse project that allows to control a Core-USB-HID-Mouse via the Spark cloud to also read PS/2 hardware (mouse/keyboard) to allow to build the Core into a mouse or keyboard, which still would work as expected but can also receive remote commands and send them on to the host computer.

So your extension to allow both mouse and keyboard at the same time is quite valuable for my project - Thanks.

Maybe @satishgn and @mohit might also want to have a look into your code and give the (sort of) stalled USB HID feature branch a new push :wink:

@satishgn would know more about this feature definitely — we stalled on it last year waiting for OpenMoko to give us another USB product ID. They never responded after many follow-ups.

We now have our own USB vendor ID, so this can move forward again. It might already be incorporated into our firmware for the Photon, but Satish will have to confirm.

1 Like

@ryotsuke @zachary, there is no HID support built into photon currently as the USB peripheral on photon is different and so require some amount of porting work to get the Keyboard/Mouse HID working on the newer photon.

Now since we have our own USB vendor ID, we can assign a new product ID for HID devices.

Will discuss with the team internally to start the groundwork on HID porting in one of our sprint.


I’d also repeat my proposal (from older threads) to go for a three(+)fold composit USB HID (CDC Serial, Keyboard, Mouse, …).

Not having to sacrifice the Serial object when using any USB HID would not only be nice, but would also help debugging during HID library/application development :wink:


I was facing some performance issues so I’ve tried experimenting with timing
Setting this

0x04,   /*bInterval: Polling Interval (4 ms) line 208, usb_desc.cpp */

Allows to use 4ms delay for mouse events and 8ms for keyboard instead of 32 for both. (lower values result in skipping events)
This increases performance pretty well.

I also agree to @ScruffR that 2 way communication is still needed, but I think HID communication is much simpler to implement as a start point. And it will consumes less space.

Though not as easy to use as Serial

One more trick to speed up keypresses
I’ll reupload code bit later


@zachary and @satishgn, guys is there an update on HID support for the Photon? This is important for a project I’m working on but there don’t seem to be any updates since early March.

Any progress would be hugely appreciated!

I just checked with some of the team, and it sounds like HID support isn’t ready yet. Thanks for the ping @sscirrus; I’ll keep my eye on HID support in the priority list and get it out soon. It’s already on our internal backlog; I just added a link to this thread there along with a note that you specifically care about it.

We’ll definitely prioritize newly discovered Photon & Core bugs first, but once things settle down, feature development will continue. :+1: And of course, pull requests welcome as always if you need it sooner. :smile:

1 Like

I moved a post to a new topic: Keyboard input for Particle