Autohotkey 自动热键如何激活上次打开的更改其pid的窗口
我正在尝试设置一个热键,以便始终启动一个新的WindowsTerminal窗口并使其具有焦点。即使存在一个现有窗口,我也总是希望它创建一个新窗口。我可以让新窗口运行,但我很难使它成为活动窗口 在Autohotkey 自动热键如何激活上次打开的更改其pid的窗口,autohotkey,Autohotkey,我正在尝试设置一个热键,以便始终启动一个新的WindowsTerminal窗口并使其具有焦点。即使存在一个现有窗口,我也总是希望它创建一个新窗口。我可以让新窗口运行,但我很难使它成为活动窗口 在Run命令期间捕获pid并使用ahk_pid似乎不起作用,因为pid在启动和出现的活动窗口之间发生变化(msgbox调试显示一个pid,window Spy显示不同的pid)。使用WinWait,ahk_exe WindowsTerminal.exe似乎会立即返回并抓取一个预先存在的窗口 #t:: Run
Run
命令期间捕获pid并使用ahk_pid
似乎不起作用,因为pid在启动和出现的活动窗口之间发生变化(msgbox调试显示一个pid,window Spy显示不同的pid)。使用WinWait,ahk_exe WindowsTerminal.exe似乎会立即返回并抓取一个预先存在的窗口
#t::
Run, wt.exe, , , TermPid
;; TermPid seems to change between the launch and the final window that appears
WinWait, ahk_pid %TermPid%, , 3
if ErrorLevel {
MsgBox, WinWait timed out... %TermPid%
return
} else {
WinActivate
}
;; problems when there are other already existing WindowsTerminal.exe windows
;WinWait, ahk_exe WindowsTerminal.exe, , 3
;if ErrorLevel {
; MsgBox, WinWait timed out...
; return
;} else {
; WinActivate
;}
return
创建新窗口后,您可以使用激活该窗口:
#Persistent
SetBatchLines, -1
Process, Priority,, High
#SingleInstance Force
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return
#t:: Run, wt.exe
ShellMessage( wParam,lParam ) {
If ( wParam = 1 ) ; HSHELL_WINDOWCREATED := 1
{
WinGet, ProcName, ProcessName, ahk_id %lParam%
If ( ProcName = "WindowsTerminal.exe" )
WinActivate, ahk_id %lParam%
}
}
我试图创建包含PID的前后数组,以确定哪个进程是新创建的进程,但针对下面创建的数组中的最后一个PID似乎可以按原样工作 代码:
#t::
Run, wt.exe
;If you are on a slow computer, you may need to increase this delay
Sleep 100 ;Forgot how to optimize this w/ WinWait, but not important rn bc its not critical
newArray:=[]
for proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process"){
if(proc.Name=="WindowsTerminal.exe"){
prID := proc.ProcessId
newArray.Push(prID)
}
}
LastItem:=newArray[newArray.MaxIndex()]
WinActivate, ahk_pid %LastItem%
return