简体   繁体   English

在 Python 中使用 Win32api 获取鼠标滚轮滚动

[英]Get Mouse Wheel Scroll usin Win32api in Python

I want to read mouse wheel scroll events and then simulate them.我想读取鼠标滚轮滚动事件,然后模拟它们。 I know I can simulate it using below code.我知道我可以使用下面的代码来模拟它。

#Scroll one up
win32api.mouse_event(MOUSEEVENTF_WHEEL, x, y, 1, 0)

#Scroll one down
win32api.mouse_event(MOUSEEVENTF_WHEEL, x, y, -1, 0)

However, I couldn't find a way to get whell scroll event using win32api in python.但是,我找不到在 python 中使用 win32api 获取滚动事件的方法。 Is there anyway to detect wheel scroll up or down events?有没有办法检测滚轮向上或向下滚动事件?

If you need to get the global WM_MOUSEWHEEL message, you can use the SetWindowsHookEx function and with WH_MOUSE_LL hook.如果您需要获取全局WM_MOUSEWHEEL消息,可以使用SetWindowsHookEx function 和WH_MOUSE_LL挂钩。

Then handle the WM_MOUSEWHEEL message in the hook function.然后处理钩子 function 中的WM_MOUSEWHEEL消息。

Here is a sample:这是一个示例:

import win32api 
import win32con
import ctypes
from ctypes import windll, CFUNCTYPE, POINTER, c_int, c_void_p, byref

user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32

def LowLevelMouseProc(nCode, wParam, lParam):
    if wParam == win32con.WM_MOUSEWHEEL:
        print("mousewheel triggerd!")
    return windll.user32.CallNextHookEx(hook_id, nCode, wParam, lParam)

if __name__ == '__main__':
    CMPFUNC = CFUNCTYPE(c_void_p, c_int, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM)

    pointer = CMPFUNC(LowLevelMouseProc)
    hook_id = user32.SetWindowsHookExW(win32con.WH_MOUSE_LL,pointer,c_void_p(win32api.GetModuleHandle(None), 0)
    msg = ctypes.wintypes.MSG()
    while user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0:
        user32.TranslateMessage(msg)
        user32.DispatchMessageW(msg)

It works for me:这个对我有用:

在此处输入图像描述

Edit编辑

If there are problems like <class'OverflowError'>: int too long to convert , you can try the following code:如果出现<class'OverflowError'>: int too long to convert等问题,可以试试下面的代码:

import win32api 
import win32con
import ctypes
from ctypes import windll, CFUNCTYPE, c_int, c_void_p, wintypes

 

user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32
user32.CallNextHookEx.argtypes = [ctypes.wintypes.HHOOK,c_int, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM]

 

def LowLevelMouseProc(nCode, wParam, lParam):
    if wParam == win32con.WM_MOUSEWHEEL:
        print("mousewheel triggerd!")
    return user32.CallNextHookEx(hook_id, nCode, wParam, lParam)

 

if __name__ == '__main__':
    CMPFUNC = CFUNCTYPE(c_void_p, c_int, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM)
    user32.SetWindowsHookExW.argtypes = [c_int,CMPFUNC,ctypes.wintypes.HINSTANCE,ctypes.wintypes.DWORD]
    pointer = CMPFUNC(LowLevelMouseProc)
    hook_id = user32.SetWindowsHookExW(win32con.WH_MOUSE_LL,pointer,win32api.GetModuleHandle(None), 0)
    msg = ctypes.wintypes.MSG()
    while user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0:
        user32.TranslateMessage(msg)
        user32.DispatchMessageW(msg)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM