vince100

detect fast mouse click

4 posts in this topic

#1 ·  Posted (edited)

I am trying to detect a mouse left click event anywhere on the screen (no GUI).

_IsPressed() when combined with a loop/sleep could not detect a click if the click is fast enough to beat the sleep time.

What alternatives I can use to make sure a mouse click event is captured?

Thanks

Edited by vince100

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You can do it with Hook:

#include <WinAPI.au3>
#include <WindowsConstants.au3>
OnAutoItExitRegister('OnAutoItExit')
HotKeySet('{ESC}', '_Exit')

Global Const $HC_ACTION = 0
Global $hStub_MouseProc = DllCallbackRegister("_MouseProc", "long", "int;wparam;lparam")
Global $hmod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hStub_MouseProc), $hmod)

While True
    Sleep(50)
WEnd

Func _MouseProc($nCode, $wParam, $lParam)
    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    If $nCode = $HC_ACTION Then
        Switch $wParam
            Case $WM_LBUTTONDOWN
                ConsoleWrite('Left MouseDown' & @CRLF)
            Case $WM_RBUTTONDOWN
                ConsoleWrite('Right MouseDown' & @CRLF)
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc

Func _Exit()
    Exit
EndFunc

Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_MouseProc)
EndFunc   ;==>OnAutoItExit
Edited by BugFix

Best Regards BugFix  

Share this post


Link to post
Share on other sites

thanks a lot, BugFix.

Your code is very neat and _WinAPI_SetWindowsHookEx is exactly what I needed.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I've been using the BugFix code for more than a year for a small functionality where I let a click of my right mouse button do a AdlibRegister("_function", 500) that after running for a few seconds unregisters itself again.

I also have a Pause button in my script and when pausing I can disable the functionality by using the supplied ...

_WinAPI_UnhookWindowsHookEx($hHook)
DllCallbackFree($hStub_MouseProc)

My problem is unpausing it. I have tried combinations of the supplied...

Global $hStub_MouseProc = DllCallbackRegister("_MouseProc", "long", "int;wparam;lparam")
Global $hmod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hStub_MouseProc), $hmod)

... but it either doesn't reactivate or crash AutoIt.

So, I'm either using too much code to Pause it, not reactivating it the proper way, or both. Please advise :)

-

I've also been noticing that the functionality is somehow disabled after a while (like 30 - 120 mins usually). After more than a year, I still haven't figured out what might be triggering that. I do almost always have PowerPro running too that may clash with it...? (although I think I tested without PowerPro and it still happened)

So I either want to reactivate/refresh this functionality on a regular basis in my script to make sure it is available, or learn of a possible alternative for this script (or bug in it).

TIA! :)

Edited by Guy_

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now