Get Mouse Double Click with pyHook
I'm trying to get Mouse Double Clicks with pyHook, but instead I'm getting two pair entries of single clicks e.g. WM_LBUTTONDOWN (0x201) and WM_LBUTTONUP (0x202) . I'm expecting WM_LBUTTONDBLCLK (0x203). What am I misssing here?
import pythoncom, pyHook
def OnMouseEvent(event):
print event.Message, event.Position
return True
hm = pyHook.HookManager()
hm.MouseAll = OnMouseE开发者_高级运维vent
hm.HookMouse()
pythoncom.PumpMessages()
I've found this hint: In this post MrZebra says: "For this (0x203) to be sent, your window class needs to be created with the CS_DBLCLKS class style"
Edit: This page indicates that doubleclicking will generate these 4 msgs: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, and WM_LBUTTONUP, but I actually get the first two twice.
I'm using Python 2.7 and pyHook 1.5.1 on Win7x64
ReEdit: I'll consider as answers C++/C# Keyboard/Mouse hooks alternatives. On the other hand, I'm not sure if it is a common practice to build functions to determine the double click event evaluating the time between two clicks.
The reason why you are getting two WM_LBUTTONDOWN
and WM_LBUTTONUP
and no WM_LBUTTONDBLCLK
is because of what MrZebra says: the target window class' style does not include the CS_DBLCLKS
style. Unless you can modify the source code for the target window class, it will never receive WM_LBUTTONDBLCLK
, because Windows will always break the double clicks into two separate clicks.
The only way to resolve this, is to measure the time between clicks. If the clicks come fast enough, then act as if you have received WM_LBUTTONDBLCLK
. See this for reading the double click time on Windows. Don't just assume that double clicks are 50 ms, or whatever.
精彩评论