首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python读取HID

python读取HID
EN

Stack Overflow用户
提问于 2011-08-10 22:18:07
回答 1查看 2.1K关注 0票数 3

我想做一个程序,从连接到linux系统的HID中获取输入,并从这些输入中生成MIDI。我在MIDI方面还好,但在隐藏方面我很困难。虽然这种方法工作正常(取自here):

代码语言:javascript
复制
#!/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中做到这一点呢?我最好不要使用非维护或旧的库,因为我希望能够分发这些代码,并让它在现代发行版上工作(所以最终的依赖项应该很容易在最终用户的包管理器中可用)

EN

回答 1

Stack Overflow用户

发布于 2011-11-07 06:02:29

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7012282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档