Get mouse deltas using Python! (in Linux)

后端 未结 3 1641
感情败类
感情败类 2020-11-30 04:21

I know that Linux gives out a 9-bit 2\'s compliment 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

相关标签:
3条回答
  • 2020-11-30 04:55

    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();
    
    0 讨论(0)
  • 2020-11-30 05:02

    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.

    0 讨论(0)
  • 2020-11-30 05:05

    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.

    0 讨论(0)
提交回复
热议问题