【发布时间】:2023-03-20 13:55:02
【问题描述】:
对于家用机器人项目,我需要读出原始鼠标移动信息。我通过使用来自this SO-answer 的python 脚本部分地成功了。它基本上读出 /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) )
while True:
getMouseEvent()
file.close()
这很好用,只是缺少滚轮信息。有人知道我如何从 /dev/input/mice 获取(最好是使用 python)滚轮信息吗?
[编辑] 好的,虽然我没能读出 /dev/input/mice,但我想我找到了解决方案。我刚刚找到了 evdev 模块(sudo pip install evdev),您可以使用它读出输入事件。我现在有以下代码:
from evdev import InputDevice
from select import select
dev = InputDevice('/dev/input/event3') # This can be any other event number. On my Raspi it turned out to be event0
while True:
r,w,x = select([dev], [], [])
for event in dev.read():
# The event.code for a scroll wheel event is 8, so I do the following
if event.code == 8:
print(event.value)
我现在要在我的 raspi 上测试它,看看它是如何工作的。感谢所有的灵感男孩和女孩!
【问题讨论】:
-
感谢您的解决方案!要找出您需要哪个 /dev/input/eventX,您可以运行 'cat /proc/bus/input/devices'
标签: python linux input mouse mousewheel