WH_MOUSE_LL 选择性响应触控板滚动

49 天前
 Leon6868

我正在用 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 一致。

请问我应该用什么方法监听触控板滚动呢?(提前感谢大佬的回答)

516 次点击
所在节点    Windows
2 条回复
ysc3839
49 天前
怀疑是 X-Y Problem https://coolshell.cn/articles/10804.html
为什么要监听触控板滚动呢?原始需求是什么?
Leon6868
49 天前
@ysc3839 #1 需求就是记录人机交互数据,包含鼠标操作和键盘操作,触控板滚动与鼠标操作中的滚轮事件在意图上是一样的,所以需要记录。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1038692

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX