首页 > 解决方案 > 如何在自动热键中使窗口成为焦点

问题描述

我想知道是否可以制作一个脚本,每 12 分钟将一个特定的应用程序聚焦,然后立即将其最小化。

所以总结一下:

到目前为止,我只发现最小化它会使以前的应用程序再次成为焦点。

标签: autohotkey

解决方案


是的,这绝对是你可以在 AutoHotkey 中做的事情。下面的链接指向您提到的特定项目的 AutoHotkey 帮助文档。

  • 等待 12 分钟:

为此,您至少有几个选择。您可以使用 and 的组合LoopSleep单独使用SetTimer。我会推荐SetTimer,但熟悉其他两个也是有益的。

https://www.autohotkey.com/docs/commands/Loop.htm

https://www.autohotkey.com/docs/commands/Sleep.htm

https://www.autohotkey.com/docs/commands/SetTimer.htm

  • 使应用程序成为焦点
  • 最小化它(或者如果可能的话,让它重新关注上次使用的应用程序?)

AutoHotkey 中有很多窗口命令。这两个是针对您特别要求的:

https://www.autohotkey.com/docs/commands/WinActivate.htm

https://www.autohotkey.com/docs/commands/WinMinimize.htm

根据您需要聚焦窗口的原因,可能会有不同的方式来完成您需要的工作。如果您需要每 12 分钟在某个窗口中输入内容,您也可以使用ControlSend而无需激活它。

这是一个帮助您入门的示例:

f1:: ; f1 will toggle your script to run
bT := !bT ; this is the toggle variable
If bT
{
    GoSub , lTimer ; triggers the timer sub to run immediately since SetTimer has to wait for period to expire on first run
    SetTimer , lTimer , 2500 ; this is in milliseconds, 12min = 720000ms
}
Else
    SetTimer , lTimer , Off
Return

lTimer: ; timer sub
If WinExist( "EEHotkeys" ) ; change EEHotkeys to be the name of your window in all places shown
{
    WinActivate , EEHotkeys
    WinWaitActive , EEHotkeys
    WinMinimize , EEHotkeys
}
Return

编辑:正如 samthecodingman 在评论中所建议的,您也可以获取活动窗口的标题,激活您的窗口,然后重新激活原始窗口。

lTimer: ; timer sub
If WinExist( "EEHotkeys" ) ; change EEHotkeys to be the name of your window in all places shown
{
    WinGetActiveTitle , sActiveWindow
    WinActivate , EEHotkeys
    WinWaitActive , EEHotkeys
    WinActivate , %sActiveWindow%
}
Return

推荐阅读