激活特定窗口并向其发送击键的最合适方法如下:
!+a::
SetTitleMatchMode, 2 ; if you want to use it only in this hotkey
IfWinNotExist, Brave
{
MsgBox, Cannot find Brave
Return ; end of the hotkey's code
}
; otherwise:
WinActivate, Brave
WinWaitActive, Brave, ,2 ; wait 2 seconds maximally until the specified window is active
If (ErrorLevel) ; if the command timed out
{
MsgBox, Cannot activate Brave
Return
}
; otherwise:
; Sleep 300 ; needed in some programs that may not react immediately after activated
Send, ^+a
Return
否则脚本可以将击键发送到另一个窗口。
为了不在每个热键中重复整个代码,您可以创建一个函数:
!+b::
SetTitleMatchMode, 2
Activate("Brave", 2)
; Sleep 300
Send, ^+a
Return
Activate(title, seconds_to_wait){
IfWinNotExist, % title
{
MsgBox % "Cannot find """ . title . """."
Return
}
; otherwise:
WinActivate, % title
WinWaitActive, % title, ,% seconds_to_wait
If (ErrorLevel)
{
MsgBox % "Cannot activate """ . title . """."
Return
}
}