我正在用 python 的 mouse
库监测鼠标事件,代码如下:
import mouse
import keyboard
def eventHandler(event):
events.append(event)
print(event)
events = [] #This is the list where all the events will be stored
mouse.hook(eventHandler) #starting the recording
keyboard.wait("a") #Waiting for 'a' to be pressed
mouse.unhook(events.append) #Stopping the recording
我发现使用触控板滚动窗体时,只有在 notepad 内滚动才会触发 WheelEvent
,在 VScode 、Explorer 、Chrome 等窗口内都不会触发 WheelEvent
。
检查 mouse
库,监听事件的 核心代码 如下(省略了不重要的代码且添加 print ):
def listen(queue):
def low_level_mouse_handler(nCode, wParam, lParam):
global previous_button_event
print(nCode, wParam, lParam.contents)
# ...
return CallNextHookEx(NULL, nCode, wParam, lParam)
WH_MOUSE_LL = c_int(14)
mouse_callback = LowLevelMouseProc(low_level_mouse_handler)
mouse_hook = SetWindowsHookEx(WH_MOUSE_LL, mouse_callback, NULL, NULL)
atexit.register(UnhookWindowsHookEx, mouse_hook)
msg = LPMSG()
while not GetMessage(msg, NULL, NULL, NULL):
TranslateMessage(msg)
DispatchMessage(msg)
我发现使用触控板滚动确实不会触发回调函数,只有在 notepad 窗体中会触发(可能是微软做了兼容?)。
pynput
的行为与 mouse
一致。
请问我应该用什么方法监听触控板滚动呢?(提前感谢大佬的回答)
1
ysc3839 217 天前 via Android
怀疑是 X-Y Problem https://coolshell.cn/articles/10804.html
为什么要监听触控板滚动呢?原始需求是什么? |