AutoHotKey select pop-up window 不工作

AutoHotKey select pop-up window not work

我写了一个脚本来测试 selecting pop-up windows。

SetTitleMatchMode, 2

winTitle:="RGui (64-bit) ahk_class Rgui Workspace ahk_exe Rgui.exe"
popWin:="ahk_class #32770 ahk_exe Rgui.exe"
IfWinExist,%winTitle%
{
    WinActivate
    send !{F4}
}

IfWinExist,%popWin%
{
    WinActivate
    WinWaitActive, %popWin%
    WinGetClass, outputvar, %popWin%
    MsgBox %outputvar%
}

这个脚本的目的是发送ALT-F4关闭一个打开的R window,当确认pop-up window发生时,显示pop-up window的class名字。

第一个 if 块工作正常。但是,发送 if 块有时有效,有时无效。活动 window 信息显示 pop-up windows' class 信息是:

Window 标题,Class 和流程

Question
ahk_class #32770
ahk_exe Rgui.exe

snapshot of the above info

我不知道为什么 IfWinExist,%popWin% 不起作用。我尝试将 popWin:="ahk_class #32770 ahk_exe Rgui.exe" 更改为 popWin:="ahk_class #32770",但它有时有效,有时无效。那么我应该如何正确地 select pop-up windows 呢?

我已经更改了您的 AutoHotkey 代码,它应该可以为您提供所需的功能。

SetTitleMatchMode, 2

winTitle:="RGui (64-bit) ahk_class Rgui Workspace ahk_exe Rgui.exe"
popWin:="ahk_class #32770 ahk_exe Rgui.exe"

if (hWnd := WinExist(winTitle)) ;this assigns hWnd, it does not compare hWnd with WinExist(winTitle)
{
    ;WinActivate, ahk_id %hWnd%
    ;send !{F4}
    WinClose, ahk_id %hWnd% ;WinClose is more direct than Alt+F4 if it works (Send can potentially send key presses to the wrong window if a new window suddenly appears)
}

WinWait, %popWin%, , 5 ;wait for window to exist, give up after 5 seconds
if !ErrorLevel ;if window found within 5 seconds
{
    WinGet, hWnd, ID, %popWin%
    WinActivate, ahk_id %hWnd%
    WinGetClass, outputvar, ahk_id %hWnd%
    MsgBox %outputvar%
}

注意: 在大多数情况下,WinActivate 需要指定 window title/hWnd。

您的代码的第二部分有时有效,但其他时候无效,可能是因为如果弹出窗口出现得非常快,那么 IfWinExist 会找到 window,但如果弹出窗口出现得很慢,那么 IfWinExist 检查将在 window 存在之前发生,因此不会找到 window.