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

2024/10/3 8:21:26

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 & 0x1bMiddle = ( button & 0x4 ) > 0bRight = ( button & 0x2 ) > 0x,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 followingif 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!

Answer

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://en.xdnf.cn/q/70745.html

Related Q&A

Tell me why this does not end up with a timeout error (selenium 2 webdriver)?

from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWaitbrowser = webdriver.Firefox()browser.get("http://testsite.com")element = WebDriverWait(browser, 10).until…

PEP 8: comparison to True should be if cond is True: or if cond:

PyCharm is throwing a warning when I do np.where(temp == True)My full code:from numpy import where, arraya = array([[0.4682], [0.5318]]) b = array([[0.29828851, 0., 0.28676873, 0., 0., 0., 0., 0.288014…

Getting the title of youtube video in pytube3?

I am trying to build an app to download YouTube videos in python using pytube3. But I am unable to retrieve the title of the video. Here goes my code: from pytube import YouTube yt = YouTube(link) prin…

pandas - concat with columns of same categories turns to object

I want to concatenate two dataframes with category-type columns, by first adding the missing categories to each column.df = pd.DataFrame({"a": pd.Categorical(["foo", "foo"…

Python convert Excel File (xls or xlsx) to/from ODS

Ive been scouring the net to find a Python library or tool that can converts an Excel file to/from ODS format, but havent been able to come across anything. I need the ability to input and output data …

Select pandas frame rows based on two columns values

I wish to select some specific rows based on two column values. For example:d = {user : [1., 2., 3., 4] ,item : [5., 6., 7., 8.],f1 : [9., 16., 17., 18.], f2:[4,5,6,5], f3:[4,5,5,8]} df = pd.DataFrame(…

Using scipy sparse matrices to solve system of equations

This is a follow up to How to set up and solve simultaneous equations in python but I feel deserves its own reputation points for any answer.For a fixed integer n, I have a set of 2(n-1) simultaneous e…

Segmentation Fault in Pandas read_csv

I have Python 2.7.5 on Os X 10.9 with Pandas version 0.12.0-943-gaef5061. When I download this train.csv file and run read_csv, I get Segmentation Fault 11. I have experimented with the file encoding…

Multiple subprocesses with timeouts

Im using a recipe that relies on SIGALRM to set alarm interrupt -- Using module subprocess with timeoutThe problem is that I have more than one Python script using signal.ALARM process to set time-outs…

what is the difference between tfidf vectorizer and tfidf transformer

I know that the formula for tfidf vectorizer is Count of word/Total count * log(Number of documents / no.of documents where word is present)I saw theres tfidf transformer in the scikit learn and I just…