首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python win32api上获取全局滚动事件

如何在python win32api上获取全局滚动事件
EN

Stack Overflow用户
提问于 2020-12-19 14:14:36
回答 1查看 78关注 0票数 0

如何在Python上使用win32api获取全局滚动事件?我搜索了how can I do,找到了答案:https://stackoverflow.com/a/65101276/8705882,但由于一个错误,它无法工作。

代码语言:javascript
复制
Traceback (most recent call last):
  File "H:/programing/p_python/woweyscroll/study1.py", line 21, in <module>
    hook_id = user32.SetWindowsHookExW(
ctypes.ArgumentError: argument 3: <class 'OverflowError'>: int too long to convert

如果有解决此错误的方法或获取全局鼠标滚动事件的其他方法,请让我知道!

EN

回答 1

Stack Overflow用户

发布于 2020-12-21 10:17:40

如果在64位系统上运行,则使用win32api.GetModuleHandle(None)参数将导致此错误。

原因是该函数将参数识别为can,您可以将其修改为c_void_p(win32api.GetModuleHandle(None)来解决此问题。

代码语言:javascript
复制
hook_id = user32.SetWindowsHookExW(win32con.WH_MOUSE_LL,pointer,c_void_p(win32api.GetModuleHandle(None)), 0)

这可以在32位和64位系统下正常工作。

编辑

代码语言:javascript
复制
import win32api 
import win32con
import ctypes
from ctypes import windll, CFUNCTYPE, c_int, c_void_p

 

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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65367195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档