使用Python获取鼠标增量! (在Linux中) [英] Get mouse deltas using Python! (in Linux)

查看:122
本文介绍了使用Python获取鼠标增量! (在Linux中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 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". My question is :

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

Can you please tell me 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 in root.)

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

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

推荐答案

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

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

因此,这是我最简单的解码代码部分,可从已弃用的"'/dev/input/mice'进行解释:

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();

这篇关于使用Python获取鼠标增量! (在Linux中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆