使用 Python 获取鼠标增量!(在 Linux 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4855823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:41:21  来源:igfitidea点击:

Get mouse deltas using Python! (in Linux)

pythonlinuxmousehid

提问by JohnRoach

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 :

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

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

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

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

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

采纳答案by Alexandre Mazel

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

我使用的是基本设备,无法访问 X 或...所以 event.py 不起作用。

So here's my simpler decode code part to interpret from "deprecated" '/dev/input/mice':

所以这是我更简单的解码代码部分,用于从“已弃用”的“/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();

回答by Rafe Kettler

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 可以读取二进制形式的文件。'b'打开文件时只需使用标志,例如open('dev/input/mice', 'rb').

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

Python 还支持所有典型的按位算术运算:移位、反转、按位与、或、异或、与非等。

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

不过,使用库来处理这些数据可能会更好地为您服务,而不是自己处理。

回答by Keith

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 moduleYou can use that, or start from there.

来自输入系统的数据以结构形式出现,而不是简单的整数。我相信鼠标设备已被弃用。首选方法是事件设备接口,其中还可以获得鼠标(和其他)输入事件。我写了一些代码来做到这一点,Event.py 模块你可以使用它,或者从那里开始。