python
  • linux
  • hid
  • 2011-08-10 147 views 3 likes 
    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?我最好不要使用非维护或旧的库,因为我希望能够分发这些代码并使其在现代发行版上工作(因此最终用户的包管理器中最终的依赖项应易于使用)

    回答

    1

    有很多事件不符合您的要求。您必须按类型或代码过滤事件:

    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) 
    
    相关问题