如何从/dev/input/mice读取滚轮信息? [英] How to read out scroll wheel info from /dev/input/mice?

查看:281
本文介绍了如何从/dev/input/mice读取滚轮信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于家庭机器人项目,我需要读取原始的鼠标移动信息.通过使用此SO-answer 中的python脚本,我在此方面取得了部分成功.它基本上读取/dev/input/mice并将十六进制输入转换为整数:

For a home robotics project I need to read out the raw mouse movement information. I partially succeeded in this by using the python script from this SO-answer. It basically reads out /dev/input/mice and converts the hex-input into integers:

import struct
file = open( "/dev/input/mice", "rb" )

def getMouseEvent():
  buf = file.read(3)
  button = ord( buf[0] )
  bLeft = button & 0x1
  bMiddle = ( button & 0x4 ) > 0
  bRight = ( button & 0x2 ) > 0
  x,y = struct.unpack( "bb", buf[1:] )
  print ("L:%d, M: %d, R: %d, x: %d, y: %d\n" % (bLeft,bMiddle,bRight, x, y) )

while True:
  getMouseEvent()
file.close()

这很好,除了缺少滚轮信息.有人知道我如何从/dev/input/mice获取(最好是使用python)滚轮信息吗?

This works fine, except for the fact that the scroll wheel information is missing. Does anybody know how I can get (preferably with python) the scroll wheel information from /dev/input/mice?

好的,尽管我没有设法读出/dev/input/mice,但我想我找到了解决方案.我刚刚找到了evdev模块(sudo pip install evdev),您可以使用该模块读取输入事件.我现在有以下代码:

Okay, although I didn't manage to read out the /dev/input/mice, I think I found a solution. I just found the evdev module (sudo pip install evdev) with which you can read out input events. I now have the following code:

from evdev import InputDevice
from select import select
dev = InputDevice('/dev/input/event3') # This can be any other event number. On my Raspi it turned out to be event0
while True:
    r,w,x = select([dev], [], [])
    for event in dev.read():
        # The event.code for a scroll wheel event is 8, so I do the following
        if event.code == 8:
            print(event.value)

我现在要在raspi上对其进行测试,看看它是如何工作的.感谢所有启发男孩和女孩!

I'm now going to test this on my raspi and see how that works. Thanks for all the inspiration guys and girls!

推荐答案

如果/dev/input/mice中每个事件只有3个字节,则表示您的鼠标配置为无轮PS/2鼠标.如果将鼠标配置为IMPS/2鼠标,则每个事件/dev/input/mice中应该有第四个字节.最后一个字节将包含车轮信息.

If you only have 3 bytes per event in /dev/input/mice, it means your mouse is configured as a wheel-less PS/2 mouse. If you configure your mouse as a IMPS/2 mouse, there should be a fourth byte in /dev/input/mice for each event. The last byte would contain the wheel information.

这篇关于如何从/dev/input/mice读取滚轮信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆