4am*_*vim 3 cmd python-3.x setforegroundwindow windows-10 alacritty
我用来pygetwindow通过它的标题将一个窗口带到前台。如果我从 cmd(和 PowerShell)运行 python 脚本,它会完美地工作。但是,如果我从任何其他终端(例如 Alacritty)运行它,提到的窗口不会进入前台,而是开始在任务栏中闪烁。
为什么会这样呢?我已将 Alacritty 配置为使用 cmd。
以下是我的代码的相关部分:
import pygetwindow
try:
window = pygetwindow.getWindowsWithTitle('foobar')[0]
window.activate()
except Exception as e:
#raise e
print("open foobar please")
exit(1)
Run Code Online (Sandbox Code Playgroud)
以下是我的 alacritty 配置的相关部分
# Shell
# You can set `shell.program` to the path of your favorite shell, e.g. `/bin/fish`.
# Entries in `shell.args` are passed unmodified as arguments to the shell.
# Default:
# - (macOS) /bin/bash --login
# - (Linux/BSD) user login shell
# - (Windows) powershell
shell:
program: cmd.exe
args:
- /s /k pushd C:\Users\interesting\bug\hunt\Repos
Run Code Online (Sandbox Code Playgroud)
谢谢
您的程序必须符合以下条件之一才能设置活动窗口(使用 API 调用SetForegroundWindow)。
您可能会发现使用 SendKeys 发送系统击键(如 Ctrl+Tab)对您有用,但您需要能够预测您的 z 顺序。
\n\n\n系统限制哪些进程可以设置前台窗口。仅当满足下列条件之一时,\n进程才能设置前台窗口:\n
\n\xe2\x80\xa2进程为前台进程。
\n\xe2\x80\xa2进程是由前台进程启动的。
\n\xe2\x80\xa2进程接收到最后一个输入事件。
\n\xe2\x80\xa2没有前台进程。
\n\xe2\x80\xa2进程正在调试中。
\n\xe2\x80\xa2前台进程不是现代应用程序或开始\n屏幕。
\n\xe2\x80\xa2前景未锁定(请参阅LockSetForegroundWindow)。
\n\xe2\x80\xa2 前台锁定超时已过期(请参阅 SystemParametersInfo 中的\nSPI_GETFOREGROUNDLOCKTIMEOUT)。
\n\xe2\x80\xa2没有激活的菜单。
\n
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow
\n这些限制的目的是解决程序窃取焦点的问题。因此,基本上,如果用户单击另一个窗口,则用户的选择获胜。
\n启动程序时,程序有两秒钟的时间显示窗口。如果用户在两秒内程序显示其窗口之前单击另一个窗口,则该程序仍将成为活动窗口。如果程序花费的时间超过 2 秒并且用户单击离开,则用户的窗口将成为活动窗口。
\n另请参阅https://devblogs.microsoft.com/oldnewthing/20090220-00/?p=19083进行讨论。
\n| 归档时间: |
|
| 查看次数: |
2761 次 |
| 最近记录: |