我想做一个程序,它从连接到 linux 系统的 HID 中获取输入并从中生成 MIDI。我在 MIDI 方面没问题,但我在 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。我猜是因为 while 的结构。
那么,在python中有没有更好的方法来做到这一点?我最好不要使用非维护或旧库,因为我希望能够分发此代码并让它在现代发行版上运行(因此最终的依赖项应该很容易在最终用户的包管理器中获得)