我知道Linux在/dev/input/mice中给出了9位2的补码数据。我也知道您可以通过/dev/hidraw0获得数据,其中hidraw是您的USB设备,可以从HID发出原始数据。我知道发送的数据是运动(位移)的增量而不是位置。另外,我还可以通过“cat/dev/input/mice”查看乱码数据。我的问题是:

您能使用Python语言告诉我如何读取此数据吗?我真的很喜欢以简单的整数形式获取该数据。但这已经证明很难。真正的问题是读取该死的数据。有没有办法读取位并进行位算术? (目前,我不担心与root用户相关的问题。请假定脚本在root用户中运行。)

(我的主要引用文献是http://www.computer-engineering.org/ps2mouse/)

最佳答案

我在基本设备上,无法访问X或...,因此event.py无法正常工作。

因此,这是我最简单的解码代码部分,可从“已弃用的”'/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();

关于python - 使用Python获取鼠标增量! (在Linux中),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4855823/

10-12 00:13
查看更多