0

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!

Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
Ismo
  • 47
  • 1
  • 3
  • 7

2 Answers2

0

Have you tried using ImgSearch to "dynamically" find the XY coordinates of the button and then do an if (MouseX => ImageX and MouseX =< ImageX + ImageWidth)?

Pseudo code (not tested):

Settimer, FindButton, 1000
Settitlematchmode, 2
Return

FindButton:
IfWinActive, YourAppWindowTitle
    ImageSearch, ImageX, ImageY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\ButtonImage.bmp
Return

#IfWinActive, YourAppWindowTitle
~LButton::
MouseGetPos, MouseX, MouseY
if (MouseX => ImageX and MouseX =< ImageX + ImageWidth)
{
    if (MouseY => ImageY and MouseY =< ImageY + ImageHeight)
    {
        Run your code here
    }
}
Return
#IfWinActive
Community
  • 1
  • 1
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • That's actually not a bad idea. However I rather use the handle id of that button as it's safer and don't depend on coordinates (which might change on other PCs). – Ismo Feb 28 '13 at 08:57
  • I understand and it might be worth looking for the ID solution. I tired to solve the problem with "moving" buttons (different screen, window moved on screen, etc.) with the timer. – Robert Ilbrink Feb 28 '13 at 11:36
0

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
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32