Jump to content

Detect if a button is clicked (non-Autoit GUI)

Recommended Posts

Hello again, guys. As usual I'm stuck and in need of enlightenment.

This time, I need to detect if a user clicked a button from a specific non-Autoit GUI (a open/save dialog) and what button was clicked (save or cancel). Since it's non-AutoIt GUI, the GUIGetMsg() is not usable, right?

I tried to search and read somewhere about this:

Opt("WinTitleMatchMode", 4)
if ControlClick("[ClASS:#32770]", "", "[CLASS:Button; INSTANCE:2]") = 1 then ConsoleWrite("Button clicked." & @LF)

But the "if ControlClick" script always clicks the button, not detecting if the button is pressed or not.

Now, the only thing I can think off is to register mouse hook. Should I use DllCallbackRegister("_LLMouseProc", "long", "int;wparam;lparam") that will detect each mouse click and active window.... or is there better, more efficient method to achieve this?

Any help would be appreciated.

Thank you.

Link to comment
Share on other sites


Yes, thank you, Authenticity. I think your idea (_GUICtrlButton_GetState) uses less CPU than using script that monitor mouse and window each time. I could get it to work. Strangely, it returns 108 as the state when a button is clicked... but, as long as it works, I'm happy.


I would use _GUICtrlButton_GetState() instead the mousehook or if_pressed. With _GUICtrlButton_GetState(), I only monitoring a button, instead monitoring mouse and the active window. So, _GUICtrlButton_GetState() should use less CPU.

Both of you thank you for trying to help.

Take care.

Link to comment
Share on other sites

Link to comment
Share on other sites

Look at WinControlSetEvent UDF.

In this case you can do something like this:

#include <WinControlSetEvent.au3>

HotKeySet("^q", "_Quit")

_WinControlSetEvent("[CLASS:#32770]", "Button2", "", "", "Clicked_Proc", "Button clicked.")

While 1

;Warning: blocking of this function by window messages with commands such as "Msgbox()" can lead to unexpected behavior,
;the return to the system should be as fast as possible !!!
Func Clicked_Proc($sParams, $hWnd, $nCtrlID)
    ConsoleWrite("Passed params: " & $sParams & @CRLF)

Func _Quit()



Using OS: Win 7 Professional, Using AutoIt Ver(s): /

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...


AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *





AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Thanks for the info MrCreatoR. I tried _WinControlSetEvent but for some reason my double click is effected. I had to close the script to undo. I have added the _OnAutoItExit() inside the script, but it still won't unhook the mouse. I could prevent this by using second script, but I think it would take extra CPU usage.

So, I use _GUICtrlButton_GetState. This will only work on the confirmation dialog when we are going to save file that the name already exists.

#include <WinAPI.au3>
#include <GUIButton.au3>
#include <ButtonConstants.au3>

While 1
    if _WinAPI_GetClassName(WinGetHandle("[ACTIVE]")) = "#32770" and StringInStr(WinGetText(WinGetHandle("[ACTIVE]")), "already exist") <> 0 then 
        local $hwndActive2 = WinGetHandle("[ACTIVE]"); Get active window
        ConsoleWrite("Dialog confirmation to replace file exists" & @CRLF)
        $buttonNo = ControlGetHandle("[ClASS:#32770]", "", "[CLASS:Button; INSTANCE:2]")
        while WinExists($hwndActive2)
            ConsoleWrite("No Button state is: " & _GUICtrlButton_GetState($buttonNo) & @CRLF)
            If _GUICtrlButton_GetState($buttonNo) = 108 or _GUICtrlButton_GetState($buttonNo) = 620 Then     
        ConsoleWrite("Save value is: " & $save & @CRLF)

I cannot just use If _GUICtrlButton_GetState($buttonNo) = 108 or 620 (note: on Scite the button state value is 108 if pressed, unlike notepad) to detect what the user selects. So, I use sleep(200) to wait for the dialog and check if the dialog exists or not. If after 200ms the button value is 108 or 620, the dialog doesn't exist... the script set the save value to 0, which means the user selects "No". It doesn't look smart, but it works for now.

Don't forget, to check the script you need to save a file that already exists, so the confirmation dialog appears.

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

  • Recently Browsing   0 members

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