python - 如何在 python win32api 上获取全局滚动事件
问题描述
如何在 Python 上使用 win32api 获取全局滚动事件?我搜索了如何做到这一点并找到答案:https://stackoverflow.com/a/65101276/8705882,由于错误而无法正常工作。
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
如果有如何解决此错误或获取全局鼠标滚动事件的其他方法,请告诉我!
解决方案
如果运行在 64 位系统上,使用该win32api.GetModuleHandle(None)
参数会导致此错误。
原因是该函数将参数识别为C int,您可以对其进行修改c_void_p(win32api.GetModuleHandle(None)
以解决此问题。
hook_id = user32.SetWindowsHookExW(win32con.WH_MOUSE_LL,pointer,c_void_p(win32api.GetModuleHandle(None)), 0)
这在 32 位和 64 位系统下都可以正常工作。
编辑
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)
推荐阅读
- javascript - 如何在 ReactJs 样式中有条件地调用不同的类名
- sql - 在 Hana 中使用 with 子句创建表
- python - TypeError:只能将str(不是“numpy.int64”)连接到str
- javascript - 在数组中查找字符串
- c# - 在 ASP.NET Core 2.2 中使用 InProcess 托管模型时,Serilog 不会将日志写入文件
- symfony - 实体值不持久(Doctrine - Symfony 4.1)
- python - 从 temp_pb2.py 文件生成 temp.proto 文件
- javascript - 如何更改 URL.createObjectURL 下载名称?
- angularjs - Angular 2+(Angular 7)是否有代码差异组件
- json - 如何在 Golang 中创建字典列表?