V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Leon6868
V2EX  ›  Windows

WH_MOUSE_LL 选择性响应触控板滚动

  •  1
     
  •   Leon6868 · 11 天前 · 348 次点击

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

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

    2 条回复    2024-05-08 12:25:32 +08:00
    ysc3839
        1
    ysc3839  
       11 天前 via Android
    怀疑是 X-Y Problem https://coolshell.cn/articles/10804.html
    为什么要监听触控板滚动呢?原始需求是什么?
    Leon6868
        2
    Leon6868  
    OP
       11 天前
    @ysc3839 #1 需求就是记录人机交互数据,包含鼠标操作和键盘操作,触控板滚动与鼠标操作中的滚轮事件在意图上是一样的,所以需要记录。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   860 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:24 · PVG 04:24 · LAX 13:24 · JFK 16:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.