title: How can I get raw USB keyboard data with Python?
body: <p>I am using PyUSB in Python as I will have to listen an USB port to retrieve data from an electronic card. For the moment, I have to train myself by reading direct input from a small keyboard (USB-connected) connected to a Raspberry-Pi. Of course, I do not want to read the typed String, I expect to get ASCII codes for example. I just don't get how I could read input from my USB-keyboard.</p>
  
  <p>I already found some snippets :</p>
  
  <pre><code>import usb.core
  import usb.util
  
  VENDOR_ID = 0x0922
  PRODUCT_ID = 0x8003
  
  # find the USB device
  device = usb.core.find(idVendor=VENDOR_ID,
                         idProduct=PRODUCT_ID)
  
  # use the first/default configuration
  device.set_configuration()
  # first endpoint
  endpoint = device[0][(0,0)][0]
  
  # read a data packet
  attempts = 10
  data = None
  while data is None and attempts &gt; 0:
      try:
          data = device.read(endpoint.bEndpointAddress,
                             endpoint.wMaxPacketSize)
      except usb.core.USBError as e:
          data = None
          if e.args == ('Operation timed out',):
              attempts -= 1
              continue
  
  print data
  </code></pre>
  
  <p>Either I get the error 16 "Device is busy" or nothing at all if I uncomment the following line "device.set_configuration()" which causes the "Device is busy" exception... (I did replace VENDOR_ID and PRODUCT_ID with my keyboard's ids)</p>