[ACCEPTED]-Get mouse deltas using Python! (in Linux)-hid

Accepted answer
Score: 19

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

So 2 here's my simpler decode code part to interpret 1 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();
Score: 6

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

Score: 3

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

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

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

Score: 0

You need to open your editor as a root to 4 bypass the Permissions-related Error messages 3 you might experience when wheen trying to 2 run this script. The /dev/input/mice device is only available 1 to the root.

More Related questions