微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

autohotkey如何激活更改其pid的最后打开的窗口

如何解决autohotkey如何激活更改其pid的最后打开的窗口

我试图设置一个热键以始终启动一个新的WindowsTerminal窗口并为其赋予焦点。即使存在现有窗口,我也总是希望它创建一个新窗口。我可以运行新窗口,但是很难将其设置为活动窗口。

Run命令中捕获pid并使用ahk_pid似乎不起作用,因为pid在启动和出现的活动窗口之间变化(msgBox调试显示一个pid,Window Spy显示一个pid)。不同的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

解决方法

我试图在包含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
,

您可以使用 RegisterShellHookWindow Function 来在创建新窗口后立即将其激活:

function fun(obj) {
 const {a,b,c} = obj;
 console.log("a=" + a,"b=" + b,"c=" + c);
}
obj = { a: "1",b: "2",c: "3" };
fun(obj);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。