问题是:我需要抓住鼠标滚轮的滚动事件(向上或向下),并将其从100个滚动计数减少到1个(没有那么精确)。所有这些都在树莓派3上的python 2.7中进行。然后继续滚动其他事件。我的想法是使用write()函数重新发送所有事件。我为此使用evdev。
到目前为止,滚动检测正在运行。我找不到只停止滚动事件并继续其他鼠标事件的方法。通过dev.grab(),所有事件都将停止,似乎阻止了dev.write()函数。
任何建议如何实现这一目标?
到目前为止的代码:
import evdev
from evdev import UInput, AbsInfo, InputDevice, categorize, ecodes
from pprint import pprint
import sys
import time
#get the mouse and use it in dev
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
foundMouse = None
for device in devices:
if "mouse" in device.name.lower():
foundMouse = device
print("Mouse found, using: ", device.path, device.name, device.phys)
break
if foundMouse is None:
print("No mouse found, exiting...")
sys.exit()
dev = InputDevice(device.path)
#pprint(dev.capabilities(verbose=True, absinfo=True))
#i guess these are not working because of the dev.grab()
#simulatedUserInput = UInput(dev.capabilities(), name='simulated-device', version=0x3)
simulatedUserInput = UInput()
#i guess this will block the dev.write(), bud it's not clear from the documentation
dev.grab()
scrollUps = 0
scrollDowns = 0
startingTime = time.time()
amountOfScrollsToReactOn = 10 #will be 100 or so, bud for testing just 10
for event in dev.read_loop():
#catch the scroll wheel
if event.type == evdev.ecodes.EV_REL and event.code == evdev.ecodes.REL_WHEEL:
#if event.type == 0x02 and event.code == 0x08:
currentTime = time.time()
#print((currentTime - startingTime))
if event.value > 0:
scrollUps+=1
scrollDowns = 0
elif event.value < 0:
scrollDowns+=1
scrollUps = 0
if (currentTime - startingTime) > 10:
startingTime = time.time()
scrollUps = 0
scrollDowns = 0
#if scrolled x amount of times in 10 seconds
elif (currentTime - startingTime) < 10 and (scrollUps > amountOfScrollsToReactOn or scrollDowns > amountOfScrollsToReactOn):
if scrollUps > amountOfScrollsToReactOn:
print("UP")
elif scrollDowns > amountOfScrollsToReactOn:
print("DOWN")
startingTime = time.time()
scrollDowns = 0
scrollUps = 0
#pprint(str(event.type) + " " + str(event.code) + " " + str(event.value))
#print(categorize(event))
else:
#pprint("sending: " + str(event.type) + " " + str(event.code) + " " + str(event.value))
#both below are not doing anything
#here the point is to reroute the other events then scrolling, bud any other way to archive this would be the solution
#dev.write(e.EV_KEY, e.KEY_A, 0)
simulatedUserInput.write(event.type, event.code, event.value)
simulatedUserInput.syn()