16

I know that Linux gives out a 9-bit two's complement data out of the /dev/input/mice. I also know that you can get that data via /dev/hidraw0 where hidraw is your USB device giving out raw data from the HID.

I know the data sent is the delta of the movement (displacement) rather than position. By the by I can also view gibberish data via the "cat /dev/input/mice".

By using the Python language, how can I read this data? I really rather get that data as in simple integers. But it has proven hard. The real problem is reading the damn data. Is there a way to read bits and do bit arithmetic? (Currently I'm not worrying over root user-related issues. Please assume the script is run as root.)

(My main reference was http://www.computer-engineering.org/ps2mouse/)

2
  • The link is broken (the whole www.computer-engineering.org site is broken (the domain expired)). Some alternatives are (essentially the same content by the same author, but slightly different titles and modification dates): The PS/2 Mouse/Keyboard Protocol (2003-05-09), The PS/2 Keyboard Interface (2003-04-01), and The AT-PS/2 Keyboard Interface (2001). Commented Aug 27, 2022 at 17:09
  • OK, not much about the mouse (the PS/2 interface is for both, though). Similar ones for mouse may exist. Commented Aug 27, 2022 at 17:12

5 Answers 5

20

I'm on a basic device and not having access to X or ... so event.py doesn't works.

So here's my simpler decode code part to interpret from "deprecated" '/dev/input/mice':

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) );
  # return stuffs

while( 1 ):
  getMouseEvent();
file.close();
Sign up to request clarification or add additional context in comments.

6 Comments

Note that you need to run this as root.
I've been hunting around for a structure that defines these 3 bytes, but maybe it doesn't exist. Thanks for this example.
must be string type in this line: button = ord( str(buf[0])[0] ); But how to use it without root rights?
/dev/input/mice is readable only by root. If you have one time root access, you can change rights to be "r" for user. But on the hardware I've got, file rights on this file were rewrotten after every reboot.
Thanks, this worked for me, but I had to open my editor as root to bypass the read permissions related to /dev/input/mice
|
6

The data from the input system comes out as structures, not simple integers. The mice device is deprecated, I believe. The preferred method is the event device interfaces, where the mouse (and other) input events can also be obtained. I wrote some code that does this, the Event.py module You can use that, or start from there.

5 Comments

Wow!! Nice code! I am currently looking through it. Just once question though How do you get the movement I can see mouse button codes in line 307. Can you give me a short example on how I may use this?
@john Thanks! Right now I don't have a mouse specific example, but I'll see if I can provide one. For now, There is a concrete device interface example for a PowerMate knob, which is a similar relative movement device. The self-test at the bottom is another example. Then there is a small app built with that that uses it as a volume control with onscreen display.
I just found out you sent me an answer. I'll be checking it out.Have you been able to find a mouse specific example?
The 'evdev' package seems to normalise this.
3

Yes, Python can read a file in binary form. Just use a 'b' flag when you open a file, e.g. open('dev/input/mice', 'rb').

Python also supports all the typical bitwise arithmetic operations: shifts, inversions, bitwise and, or, xor, and not, etc.

You'd probably be better served by using a library to process this data, instead of doing it on your own, though.

6 Comments

Thank you for your prompt response. One question though dev/input/mice changes continually since the mouse position changes continually is there a way to read it real time?
@JohnRoach I'm not sure. You generally don't want to be reading data that is changing as you read it, though. Again, the best idea here would be to use a library that does all the hard work for you
@Rafe Kettler I've tried using open() it didn't work. I simply used f=open('/dev/input/mice', 'rb') for line in f: print line, And it seems real time data can't be read from this file :( Can you recommend a library? Of course this was all in a loop. :)
@JohnRoach what'll happen is that it probably acquires a lock on the file and then just uses the data when the lock was acquired. That's how file IO works in most languages. You'll need something beyond just file IO to handle the mouse data in real time, sadly
@JohnRoach a list of "mouse" Python packages can be found at pypi.python.org/…; it looks like PyMouse might suit your purposes.
|
0

You need to open your editor as a root to bypass the permissions-related error messages you might experience when trying to run this script.

The /dev/input/mice device is only available to root.

3 Comments

This seems to be a comment to Alexandre Mazel's answer (the only post with a script).
How is using an editor related to running the script? Running the script from within the editor?
0

You can use evdev package. According to the document, this package manipulates with /dev/input/ and has many functions such as read the inputs forever, read one input, write an event, etc. Usage

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.