Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read out scroll wheel info from /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()

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!

like image 486
kramer65 Avatar asked Nov 13 '22 07:11

kramer65


1 Answers

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.

like image 129
zakinster Avatar answered Nov 14 '22 22:11

zakinster