I'm trying to handle events from multiple usb keyboards, so that the code knows from which keyboard an input is coming from. The code identify the different keyboards via the device instance id (they all have the same product and vendor id) but not from which an user input is coming from (it just toggles between them).
Is this even possible with pywinusb? I tried playing with the event handlers with no luck.
from time import sleep
from msvcrt import kbhit
import pywinusb.hid as hid
# feel free to test
target_vendor_id = 0xffff
target_product_id = 0x0035
def sample_handler(data):
print("Raw data: {0}".format(data))
def getinput(data, id):
print data
print id
if(id == "8&2754010&0&0000" and data == "09008708"):
print "Success"
else:
print "Failed"
def raw_test():
# example, handle the HidDeviceFilter().get_devices() result grouping items by parent ID
all_hids = hid.HidDeviceFilter(vendor_id = target_vendor_id, product_id = target_product_id).get_devices()
#print all_hids
if all_hids:
while True:
for index, device in enumerate(all_hids):
result = device.instance_id.split('\\')[-1]
try:
device.open()
getinput(raw_input(), result)
finally:
device.close()
if __name__ == '__main__':
# first be kind with local encodings
import sys
if sys.version_info >= (3,):
# as is, don't handle unicodes
unicode = str
raw_input = input
else:
# allow to show encoded strings
import codecs
sys.stdout = codecs.getwriter('mbcs')(sys.stdout)
raw_test()