在 Python 中使用 Win32api 获取鼠标滚轮滚动 [英] Get Mouse Wheel Scroll using Win32api in Python

查看:321
本文介绍了在 Python 中使用 Win32api 获取鼠标滚轮滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取鼠标滚轮滚动事件,然后模拟它们.我知道我可以使用下面的代码模拟它.

#向上滚动一个win32api.mouse_event(MOUSEEVENTF_WHEEL, x, y, 1, 0)#向下滚动一个win32api.mouse_event(MOUSEEVENTF_WHEEL, x, y, -1, 0)

但是,我找不到在 Python 中使用 win32api 获取 whell 滚动事件的方法.有没有办法检测滚轮向上或向下滚动事件?

解决方案

如果需要获取全局 WM_MOUSEWHEEL 消息,可以使用

编辑

如果出现: int too long to convert之类的问题,可以试试下面的代码:

导入win32api导入 win32con导入 ctypes从 ctypes 导入 windll、CFUNCTYPE、c_int、c_void_p、wintypesuser32 = ctypes.windll.user32kernel32 = ctypes.windll.kernel32user32.CallNextHookEx.argtypes = [ctypes.wintypes.HHOOK,c_int, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM]def LowLevelMouseProc(nCode, wParam, lParam):如果 wParam == win32con.WM_MOUSEWHEEL:打印(鼠标滚轮触发!")返回 user32.CallNextHookEx(hook_id, nCode, wParam, lParam)如果 __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]指针 = CMPFUNC(LowLevelMouseProc)hook_id = user32.SetWindowsHookExW(win32con.WH_MOUSE_LL,pointer,win32api.GetModuleHandle(None), 0)msg = ctypes.wintypes.MSG()而 user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0:user32.TranslateMessage(msg)user32.DispatchMessageW(msg)

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. Is there any way 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.

Then handle the WM_MOUSEWHEEL message in the hook function.

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:

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)

这篇关于在 Python 中使用 Win32api 获取鼠标滚轮滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆