如何从/ dev / input / mice中读出滚轮信息?

对于家庭机器人项目,我需要读出原始的鼠标移动信息。 我通过使用这个SO-answer中的python脚本部分地成功了。 它基本上读出/ dev / input / mice并将hexinput转换为整数:

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

这工作正常,除了滚轮信息丢失的事实。 有谁知道我怎么可以(最好用python)从/ dev / input / mice的滚轮信息?

[编辑]好吧,虽然我没有设法读出/ dev / input / mice,我想我find了一个解决scheme。 我刚刚find了evdev模块(sudo pip install evdev),可以读取input事件。 我现在有以下代码:

 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上testing这个,看看它是如何工作的。 感谢所有的灵感家伙和女孩!

如果您在/ dev / input / mouse中每个事件只有3个字节,则表示您的鼠标配置为无轮PS / 2鼠标。 如果将鼠标配置为IMPS / 2鼠标,则每个事件的/ dev / input / mice应该有第四个字节。 最后一个字节将包含轮子信息。