首页 > 解决方案 > 如何制作一个点击特定窗口中特定点并且在后台运行的 ahk 项目?

问题描述

我将解释我希望我的代码做什么: 1. 我将打开一个窗口 2. 我将导航到该窗口并按 control+q 3. 我将使用 Google 功能获取坐标并将它们发布到输入框 4. 代码应该多次单击该窗口中的该点(我想在后台运行代码,因为我想同时执行另一项任务)

我写了一些代码,但它没有按预期工作

#SingleInstance force
#persistent
$^q::
WinGetTitle, Title, A
MsgBox, The active window is "%Title%".
Inputbox, c1, , Coordinate of button you want to click on(x)
Inputbox, c2, , Coordinate of button you want to click on(y)
MsgBox %c1% , %c2%
Inputbox,times, ,How many times would you like to click?
While(times>0)
{
WinGet,b,,Title                 ;I think here is the problem
ControlClick, X%c1% Y%c2%,%b%    ;or here
Sleep, 10500
times--
}
MsgBox End
return

标签: clickwindowcoordinatesautohotkey

解决方案


确实有一些问题需要修复,尽管修复它们仍然不能保证您的代码能够正常工作。ControlClicking 总是会很受欢迎。

所以你最大的问题就在这里
WinGet,b,,Title
,甚至不知道这条线应该做什么。
当然,您正在尝试引用Title您在上面定义的变量,并在旧语法中执行此操作,您将其包装在%%. 也许你只是忘记了。
但即使修复了这个问题,我也不知道为什么要使用这个命令。
您甚至没有指定要使用哪个子命令,因此默认检索匹配窗口的 hwnd。
如果这确实是您想要的,那么您需要修复ControlClick的 WinTitle 参数以指定您正在匹配基于其 hwnd 的窗口,如下所示:
ControlClick, X%c1% Y%c2%, ahk_id %b%

基于它们的 hwnd 匹配窗口实际上是匹配窗口的最佳实践,但是您在这里的实现是有问题的。我什至认为这是偶然的。
尽管如此,让我将代码修复为更合理的 hwnd 匹配实现。
我还将摆脱遗留语法并切换到更新更好的表达式语法。

#SingleInstance, Force
#Persistent

$^q::
    ;ditched the legacy WinGetTitle command, and switched over to
    ;the WinActive function, its return value is the HWND of the
    ;matched window (active window)
    ActiveWindowHWND := WinActive("A")

    ;getting the title as well, in case you really want to see it
    ;this part can be removed though
    WinGetTitle, ActiveWindowTitle, % "ahk_id " ActiveWindowHWND

    ;concatenating strings and values by just separting them with a space
    ;and double quotes to escape a quote in an expression
    ;outer most quotes always just specify that we're writing a string
    MsgBox, % "The active window's HWND is " ActiveWindowHWND " and its title is """ ActiveWindowTitle """"

    ;explicitly specifying that these long strings are actually strings
    Inputbox, c1, , % "Coordinate of button you want to click on(x)"
    Inputbox, c2, , % "Coordinate of button you want to click on(y)"
    MsgBox, % c1 " , " c2 
    Inputbox, times, , % "How many times would you like to click?"
    While (times > 0)
    {
        ;specifying the NA option
        ;it's documented to improve reliablility, read more about it
        ;from the documentation
        ControlClick, % "x" c1 " y" c2, % "ahk_id " ActiveWindowHWND, , , , NA
        Sleep, 10500
        times--
    }
    MsgBox, % "End"
return

如果您完全不熟悉更新和更好的表达式语法,您可以切换回旧语法,它也可以工作。
只需确保将NA选项添加到控件单击虽然我当然会推荐更新的表达式语法。
这是一个很好的入门文档页面:
https ://www.autohotkey.com/docs/Language.htm


推荐阅读