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

1

7

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!

Circinus answered 8/4, 2013 at 15:9 Comment(1)
Thanks for your solution! To find out which /dev/input/eventX you need you can run 'cat /proc/bus/input/devices'Apotropaic
C
2

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.

Covin answered 8/4, 2013 at 15:20 Comment(6)
Ehm ok, didn't know that. And how would you suggest I could configure my mouse as a IMPS/2 mouse?Circinus
Well it's more a hardware issue than a software issue, i think you have to send some magic sequence to the mouse to get it to switch from PS/2 protocol to IMPS/2 protocol.Covin
You can find more information about the PS/2 protocol here : win.tue.nl/~aeb/linux/kbd/scancodes-13.htmlCovin
Alright, I'm just reading something about changing a file in /etc/X11/ but I'm kinda lost here..Circinus
If you're having a X11 server, why aren't you using directly XLib ? I think there is a python XLib interface.Covin
Apart from the fact that I had never heard of XLib, I run my script through ssh on a raspberry pi without a graphical user interface. It could be however, that X is loaded anyway, but I am not sure of this. Since the aim is to run it without X I'd rather not depend on it. Just for the info: the reason I want to read out mouse data without a GUI is that I let the mouse scroll wheel detect some movement, which I use as input for my robotics project. I just use the mouse as a simple and cheap sensor.. :) - But I'm just going to read about XLib now. Thanks for the tip!Circinus

© 2022 - 2024 — McMap. All rights reserved.