Хорошо, у меня есть решение сейчас, я опубликую его, но у меня есть вопрос ... когда я запускаю код, иногда он говорит, что устройство занято, и оно генерирует ошибку, и когда оно работает .. ... он будет ждать прерывания, однако, если вы будете двигать мышь, он будет оставаться неподвижным на экране, но будет вызывать прерывание. То же самое можно сказать и о нажатии на кнопку, оно будет вызывать прерывание, но мышь будет оставаться неподвижной, и чтобы использовать ее снова, вам нужно вынуть ее из usb и вставить снова.
import usb.core
import usb.util
#import usb
# find our device
dev = usb.core.find(find_all=True)
#the second device it finds is my mouse
device = dev[2]
#print device
#physical device call: 5
#usb HID call: 3
_name = usb.util.get_string(device, 19, 1)
print _name
#we take the first configuration of the device
device.set_configuration()
print "Config set..."
#we access the configuration we've found
cfg = device.get_active_configuration()
#we access the intherface with number 0 and alternate setting with number 0
interface_number = cfg[(0,0)].bInterfaceNumber
alternate_setting = usb.control.get_interface(device,interface_number)
#we find the alterng settings for interface_number and altering_setting
intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number,\
bAlternateSetting = alternate_setting)
#Finds the first IN endpoint
ep = usb.util.find_descriptor(
intf,
# match the first IN endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN
)
#inorder for you to detect a state from the device, it has to be(for mouse, moved,
#clicked)
#otherwise it generates error
#make use of the error, if the mouse isn't pushed, do nothing and wait, if pushed...
#print the state
#and exit from the loop
print "Waiting for signal..."
#device.detach_kernel_driver(0)
#click of the scroll button has array('B', [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#0, 0]) signal
while True:
try:
print ep.read(16)
print "Received!"
break
except:
continue
#assert ep is not 0
#_name=device.ctrl_transfer(bmRequestType=33, bRequest=11, wValue=0x0300)
#print _name
Итак, я предполагаю, что сначала он удаляет драйверы мыши, затем он разговаривает с устройством, затем я создаю прерывание, нажимая кнопку, а затем ... как я могу сказать, снова использовать драйверы и завершить программу ... . потому что неудобно каждый раз переставлять usb-мышь.