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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JohnRoach
  • 747
  • 4
  • 11
  • 26
  • 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](https://www.avrfreaks.net/sites/default/files/PS2%20Keyboard.pdf)* (2003-05-09), *[The PS/2 Keyboard Interface](http://www-ug.eecg.toronto.edu/msl/nios_devices/datasheets/PS2%20Keyboard%20Protocol.htm)* (2003-04-01), and *[The AT-PS/2 Keyboard Interface](https://www.tayloredge.com/reference/Interface/atkeyboard.pdf)* (2001). – Peter Mortensen Aug 27 '22 at 17:09
  • OK, not much about the mouse (the PS/2 interface is for both, though). Similar ones for mouse may exist. – Peter Mortensen Aug 27 '22 at 17:12

4 Answers4

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();
qdot
  • 6,195
  • 5
  • 44
  • 95
Alexandre Mazel
  • 2,462
  • 20
  • 26
  • 4
    Note that you need to run this as root. – srlm Jul 31 '13 at 07:17
  • I've been hunting around for a structure that defines these 3 bytes, but maybe it doesn't exist. Thanks for this example. – JustinB Apr 26 '14 at 21:18
  • 1
    must be string type in this line: `button = ord( str(buf[0])[0] );` But how to use it without root rights? – Vasin Yuriy Feb 11 '17 at 12:12
  • /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. – Alexandre Mazel Feb 13 '17 at 11:36
  • Thanks, this worked for me, but I had to open my editor as root to bypass the read permissions related to `/dev/input/mice` – MT_Shikomba Feb 17 '21 at 18:02
  • For python 3.x you can remove ord from the line and simply run `button = buf[0]` – Dhruv Aug 28 '23 at 13:25
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.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • 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? – JohnRoach Feb 02 '11 at 18:25
  • @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](http://code.google.com/p/pycopia/source/browse/trunk/core/pycopia/OS/Linux/powermate.py), which is a similar relative movement device. The self-test at the bottom is another example. Then there is a [small app](http://code.google.com/p/pycopia/source/browse/trunk/core/pycopia/OS/Linux/mastervolume.py) built with that that uses it as a volume control with onscreen display. – Keith Feb 02 '11 at 22:44
  • 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? – JohnRoach Feb 06 '11 at 10:26
  • @john Ok, [basic mouse interface](http://code.google.com/p/pycopia/source/browse/trunk/core/pycopia/OS/Linux/mouse.py). – Keith Feb 06 '11 at 21:28
  • The 'evdev' package seems to normalise this. – fuzzyTew Jun 10 '21 at 10:01
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.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 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 Jan 31 '11 at 20:51
  • @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 Jan 31 '11 at 20:53
  • @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 Jan 31 '11 at 20:59
  • @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 – Rafe Kettler Jan 31 '11 at 21:00
  • @JohnRoach a list of "mouse" Python packages can be found at http://pypi.python.org/pypi?%3Aaction=search&term=mouse&submit=search; it looks like [PyMouse](http://pypi.python.org/pypi/PyMouse/1.0) might suit your purposes. – Rafe Kettler Jan 31 '11 at 21:04
  • Ok. I did some trying out it seems I can't simply open and read the file. A sample code will be: mouse = file('/dev/input/mice') running = True while running: code = ord(mouse.read(1)) print code :::: However the problem with this it doesn't give me the overflow bit. I tried binary wrap around mouse.read(1) but it told me that the data wasn't actually binary... And than you very much for your help!! – JohnRoach Jan 31 '11 at 21:22
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MT_Shikomba
  • 129
  • 1
  • 6
  • This seems to be a comment to [Alexandre Mazel's answer](https://stackoverflow.com/questions/4855823/get-mouse-deltas-using-python-in-linux/12286738#12286738) (the only post with ***a script***). – Peter Mortensen Aug 27 '22 at 17:20
  • Related: *[Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/questions/214173/)*. – Peter Mortensen Aug 27 '22 at 17:20
  • How is using an editor related to running the script? Running the script ***from within*** the editor? – Peter Mortensen Aug 27 '22 at 17:22