Search code examples
c++keyboardusb

Select specific USB keyboard input device?


I am working on a small project: I need to take the input from a number pad and run macros based off which key was pressed. However, I'm having some difficulty reading from the number pad specifically. stdin simply reads from the key inputs; I actually need the raw data output from the number pad USB device. The number pad is a separate device from the keyboard.

My research indicates I'll have to write a device driver for it using the Windows DDK, but this doesn't quite make sense to me. I want to select a different USB input device as a keyboard - this shouldn't require a new driver, as I'll still be using it as a keyboard. I just need to change the scope of my input stream to just the USB device.

I could also be missing some key piece of information. So, is there a simple way to do this?


Solution

  • You have to read the RAW usb-events from the right device. You can write that code either yourself by reading 'Raw Input' or 'Human Interface Devices Reference' (part of the Windows Driver Kit) on MSDN.

    Or you could use libusb, as mentioned on Read Raw USB Input on Windows, which hides a lot of the crufty details under a neat API. A rough idea is to:

    • get the list of devices via libusb_get_device_list()
    • find the keyboard you are interested in via libusb_get_device_descriptor(), check the examples/listdevs.c file for a clue about how to do that.
    • open the device via libusb_open()
    • do your magic (i think polling for events will fit)
    • and .. close the device at the end

    Side note: libusb flawlessly built on Windows8 with VS2012 Express + WDK8.