AutoHotKey – ellegant way of waiting until a system dialog box becomes able to accept keystrokes?

4

1

Standard system Print dialog or Save dialog is swallowing keys sent immediately after opening. Is there a way how to send keys successfully as soon as possible?

Details:

Let's have some simple use case of Print dialog, i.e. if you press Ctrl+P in Internet Explorer. Once it opens, I just want to send Alt+p to press the Print button as soon as possible. But the following script does not work:

#IfWinActive, ahk_class IEFrame
F2::
    Send ^p
    WinWait, Print,, 2
    Send !p   ; this has no effect if sent immediately
Return
#IfWinActive

It starts working when I insert Sleep 500 before Send !p. But perhaps 500 ms won't be enough in some cases. Is there some ellegant way how to insert keystrokes ASAP?

miroxlav

Posted 2015-09-18T08:16:42.040

Reputation: 9 376

Answers

2

#IfWinActive, ahk_class IEFrame

F2:: 
    Send ^p
    WinWait, Print ahk_class #32770     ; Waits until the specified window exists
    IfWinNotActive, Print ahk_class #32770, ,WinActivate, Print ahk_class #32770
    WinWaitActive, Print ahk_class #32770   ; Waits until the specified window is active 
    Send !p
Return

#IfWinActive

or

; WinWait, WinTitle, WinText, Seconds, ExcludeTitle, ExcludeText

WinWait, Print ahk_class #32770, WinText  ; Use Window Spy to find out a single text element of the target window 
IfWinNotActive, ...
...

user3419297

Posted 2015-09-18T08:16:42.040

Reputation: 2 055

Great, works! Maybe specifying ahk_class #32770 is little bit redundant – isn't it? – miroxlav – 2015-09-18T11:18:28.390

I see the trick was is activation. Window is immediately presented as focused (active), but it seems that it actually isn't active yet. Activation solves the problem – miroxlav – 2015-09-18T11:22:15.003