python
  • linux
  • hid
  • 2011-08-10 151 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) 
    
    相關問題