3
我想做一个程序,从HID附加到Linux系统的输入,并从那些生成的MIDI。我在MIDI方面没问题,但我在HID方面苦苦挣扎。虽然这种方法确定(从here拍摄):蟒蛇阅读HID
#!/usr/bin/python2
import struct
inputDevice = "/dev/input/event0" #keyboard on my system
inputEventFormat = 'iihhi'
inputEventSize = 16
file = open(inputDevice, "rb") # standard binary file input
event = file.read(inputEventSize)
while event:
(time1, time2, type, code, value) = struct.unpack(inputEventFormat, event)
print type,code,value
event = file.read(inputEventSize)
file.close()
它得到高的时候有很多事件的CPU使用率;特别是在跟踪鼠标时,大型移动占用了我系统上近50%的CPU。我猜是因为这个时间的结构。
那么,有没有更好的方法来做到这一点在Python?我最好不要使用非维护或旧的库,因为我希望能够分发这些代码并使其在现代发行版上工作(因此最终用户的包管理器中最终的依赖项应易于使用)