2

我正在尝试处理来自多个 USB 键盘的事件,以便代码知道输入来自哪个键盘。该代码通过设备实例 ID 识别不同的键盘(它们都具有相同的产品和供应商 ID),但不是来自用户输入的来源(它只是在它们之间切换)。

这甚至可以用pywinusb吗?我尝试使用事件处理程序但没有运气。

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()
4

0 回答 0