How to read out scroll wheel info from /dev/input/mice?

♀尐吖头ヾ 提交于 2019-12-21 05:02:23

问题


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()

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?

[EDIT] 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)

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


回答1:


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.



来源:https://stackoverflow.com/questions/15882665/how-to-read-out-scroll-wheel-info-from-dev-input-mice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!