3

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?

Basj
  • 41,386
  • 99
  • 383
  • 673
Selcuk
  • 109
  • 2
  • 11

1 Answers1

2

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:

enter image description here

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)
Paul Randleman
  • 104
  • 1
  • 8
Zeus
  • 3,703
  • 3
  • 7
  • 20
  • Hi,if this answer did help to you, please feel free to mark it to help people with the same issue, and let me know if you have any problem.Thanks. – Zeus Dec 25 '20 at 08:09
  • How to distinguish up wheelscrolls and down wheelscrolls? – Basj Sep 17 '21 at 10:06
  • You should delete the 1st snippet, as it's *Undefined Behavior* ([\[SO\]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)](https://stackoverflow.com/questions/58610333/c-function-called-from-python-via-ctypes-returns-incorrect-value/58611011#58611011)). 2nd is too, it works by luck (*TranslateMessage*, *DispatchMessageW*), – CristiFati Sep 17 '21 at 10:22