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

江枫思渺然 提交于 2020-01-06 18:08:20

问题


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!


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/15128707/autohotkey-trigger-script-when-a-certain-button-was-clicked-in-a-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!