我想做一个程序,从连接到linux系统的HID中获取输入,并从这些输入中生成MIDI。我在MIDI方面还好,但在隐藏方面我很困难。虽然这种方法工作正常(取自here):
#!/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中做到这一点呢?我最好不要使用非维护或旧的库,因为我希望能够分发这些代码,并让它在现代发行版上工作(所以最终的依赖项应该很容易在最终用户的包管理器中可用)
发布于 2011-11-07 06:02:29
有很多事件不符合您的要求。您必须按类型或代码筛选事件:
while event:
(time1, time2, type, code, value) = struct.unpack(inputEventFormat, event)
if type==X and code==Y:
print type,code,value
event = file.read(inputEventSize)https://stackoverflow.com/questions/7012282
复制相似问题