Search code examples
buttonhandleautohotkeybuttonclickhwnd

Autohotkey - Trigger script when a certain button was clicked in a window


I know the actual button's handle. What I would like to check if that button was clicked and if so that would trigger an autohotkey script.

Thanks in advance!


Solution

  • You are right. You can use this instead of ImgSearch.

    ControlGetPos, x, y, w, h, button1, ahk_class CalcFrame
    MsgBox, %x% %y% %w% %h%
    return
    

    So you would have to run the ControlGetPos after each mouse click (only when the target window title is active) and then compare the actual mouse coordinates with the button click area.

    Here is some code for the calculator:

    #SingleInstance Force
    #Persistent
    
    #IfWinActive, ahk_class CalcFrame
        ~LButton::
        MouseGetPos, MouseX, MouseY
        ControlGetPos, ButtonX, ButtonY, ButtonW, ButtonH, button1, ahk_class CalcFrame
        ButtonX2:=ButtonX + ButtonW
        ButtonY2:=ButtonY + ButtonH
        if MouseX between %ButtonX% and %ButtonX2%
        {
            if MouseY between %ButtonY% and %ButtonY2%
            {
                MsgBox, You pressed the MC button
            }
        }
        Return
    #IfWinActive