Jump to content

How to get Left Click from any current window?


qwert
 Share

Recommended Posts

It's been a while since I've worked with this, but I recall that you can register a message and get clicks from other windows.

But when I use this:

GUIRegisterMsg($WM_LBUTTONDOWN, "WM_COMMAND")

I only receive clicks on my own GUI.

Can someone point me in the right direction?

Thanks in advance for any help.

Link to comment
Share on other sites

Thanks for your suggestion.

Yes, it definitely works ... but I'm finding it difficult to implement that method on a normal GUI that has to process its own buttons and has GUIGetMsg() in a While loop.

Would anyone happen to have an example of how that might be done?

 

Link to comment
Share on other sites

@qwert: I tried to write a little script based on your request and argumentum's suggestion.
It sure could be done better because I'm a total newbie when it comes to callback scripts.
Also these low-level hooks scripts seem a bit dangerous to manipulate.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPISys.au3>
#include <WinAPIConstants.au3>
#include <WindowsConstants.au3>

OnAutoItExitRegister("_UnHook")

Global $hFunc, $pFunc, $hMod, $hHook, $bHookSet = False
$hFunc = DllCallbackRegister('_MouseProc', 'lresult', 'int;int;int')
$pFunc = DllCallbackGetPtr($hFunc)
$hMod = _WinAPI_GetModuleHandle(0)

$hGUI = GUICreate("Form", 300, 200)
$idButton = GUICtrlCreateButton("Button", 100, 50, 80, 30)
$idCheckbox = GUICtrlCreateCheckbox("Checkbox", 100, 100, 80, 30)
GUISetState()

While 1
    If WinActive($hGUI) Then
        If $bHookSet Then
            _WinAPI_UnhookWindowsHookEx($hHook)
            $bHookSet = False
        EndIf
    Else ; Gui not active
        If Not $bHookSet Then
            $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, $pFunc, $hMod)
            $bHookSet = True
        EndIf
    EndIf

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $idButton
            MsgBox($MB_TOPMOST, "Test 1", "Button pressed")
        Case $idCheckbox
            MsgBox($MB_TOPMOST, "Test 2 ", "Checkbox ticked/unticked")
    EndSwitch
WEnd

;==========================================
Func _MouseProc($iCode, $iwParam, $ilParam)

    If $iCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $iCode, $iwParam, $ilParam)
    Switch $iwParam
        Case $WM_LBUTTONDOWN
            MsgBox($MB_TOPMOST, "", "Left Mouse down", 1)
    EndSwitch
    Return _WinAPI_CallNextHookEx($hHook, $iCode, $iwParam, $ilParam)

EndFunc   ;==>_MouseProc

;==========================================
Func _UnHook()

    ConsoleWrite("_UnHook" & @lf)
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hFunc)
    Exit

EndFunc   ;==>_UnHook

The approach is to unset the hook when the GUI is active, then set it again as soon as the GUI is not active. It seems to work for all left clicks... except the 1st one outside GUI

If anyone could improve this 1st click issue, you're welcome (I tried to solve it with WM_ACTIVATEAPP message... no success)

Edited by pixelsearch
deleted some superfluous comments
Link to comment
Share on other sites

In case anyone needs a simpler way "to get Left Click from any current window", let's not forget the _IsPressed way :

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Global $iInc = 0, $hDLL = DllOpen("user32.dll")

$hGUI = GUICreate("Form", 300, 200)
$idButton = GUICtrlCreateButton("Button", 100, 50, 80, 30)
$idCheckbox = GUICtrlCreateCheckbox("Checkbox", 100, 100, 80, 30)
GUISetState()

While 1
    If _IsPressed("01", $hDLL) Then ; "01" = Left mouse button
        While _IsPressed("01", $hDll)
            Sleep(10)
        Wend
        If Not WinActive($hGUI) Then
            $iInc += 1
            ConsoleWrite($iInc & " - Left click outside GUI" & @lf)
        EndIf
    EndIf

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            DllClose($hDLL)
            Exit
        Case $idButton
            MsgBox($MB_TOPMOST, "Test 1", "Button pressed")
        Case $idCheckbox
            MsgBox($MB_TOPMOST, "Test 2 ", "Checkbox ticked/unticked")
    EndSwitch
WEnd

 

Link to comment
Share on other sites

@pixelsearch: Thanks for that simple method.

I added a _WinAPI_GetCursorInfo() statement to get the X,Y coordinates of the external click and it responds properly in the test cases I've tried.

The other methods are interesting, but I'll always lean toward simple.

I appreciate your help.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...