gpt4 book ai didi

python 读取 HID

转载 作者:太空狗 更新时间:2023-10-29 11:20:23 24 4
gpt4 key购买 nike

我想做一个程序,从连接到 linux 系统的 HID 获取输入并从中生成 MIDI。我在 MIDI 方面没问题,但我在 HID 方面苦苦挣扎。虽然这种方法工作正常(取自 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 中有更好的方法吗?我最好不要使用未维护的或旧的库,因为我希望能够分发此代码并让它在现代发行版上运行(因此最终用户的包管理器中应该很容易获得最终的依赖项)

最佳答案

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

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)

关于python 读取 HID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7012282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com