nirbruner
nirbruner

Reputation: 347

libusb-win32 - can't read from keyboard

I'm trying to write a custom 'driver' for a keyboard (HID, if it matters), under Windows 7. The final goal is having two keyboards connected to the computer, but mapping all of the keys of one of them to special (custom) functions.

My idea is to use libusb-win32 as the 2nd keyboard's driver, and write a small program to read data from the keyboard and act upon it. I've successfully installed the driver, and the device is recognized from my program, but all transfers timeout, even though I'm pressing keys.

here's my code:

     struct usb_bus *busses;
     struct usb_device *dev;
     char buf[1024];

     usb_init();
     usb_find_busses();
     usb_find_devices();

     busses = usb_get_busses();
     dev = busses->devices;

     cout << dev->descriptor.idVendor << '\n' << dev->descriptor.idProduct << '\n';

     usb_dev_handle *h = usb_open(dev);
     cout << usb_set_configuration(h, 1) << '\n';
     cout << usb_claim_interface(h, 0) << '\n';
     cout << usb_interrupt_read(h, 129, buf, 1024, 5000) << '\n';
     cout << usb_strerror();
     cout << usb_release_interface(h, 0) << '\n';
     cout << usb_close(h) << '\n';

and it returns:

  1133
  49941
  0
  0
  -116
  libusb0-dll:err [_usb_reap_async] timeout error
  0
  0

(I'm pressing lots of keys in those 5 seconds)

There's only one bus, one device, one configuration, one interface and one endpoint. The endpoint has bmAttributes = 3 which implies I should use interrupt transfers (right?)

so why am I not getting anything? Am I misusing libusb? Do you know a way to do this without libusb?

Upvotes: 2

Views: 3133

Answers (1)

nirbruner
nirbruner

Reputation: 347

It's pretty simple actually - when reading from the USB device, you must read exactly the right amount of bytes. You know what that amount is by reading wMaxPacketSize.

Apparently a read request with any other size simply results in a timeout.

Upvotes: 4

Related Questions