首页 > 解决方案 > Powershell,关闭活动窗口

问题描述

我找到了一个我修改的片段以找到活动窗口并通过 ps1 脚本将其关闭。我的目标是使用 hotkeyD 使我的键绑定与 Linux 相同。

我的脚本有三个问题。一个是如果我正在浏览文件,它不会关闭活动的资源管理器窗口。我将如何做到这一点?

第二个问题是。我希望 winfetch 一启动就在终端中执行。但是,如果我将它添加到我的 powershell 配置文件中。它使脚本需要更长的时间来执行。如何使脚本省略我在文档中的 powershell 配置文件中的代码?

三是速度。脚本不是即时的。就像按 alt+F4 一样。它需要一两秒钟才能激活。我将如何加快速度?

谢谢。期待看到一些回复。这是脚本。

[CmdletBinding()]            
Param(            
)            
Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class UserWindows {
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();
}
"@            
try {            
$ActiveHandle = [UserWindows]::GetForegroundWindow()
$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle}
$Process | Select ProcessName, @{Name="AppTitle";Expression= {($_.CloseMainWindow())}}
} catch {            
 Write-Error "Failed to get active Window details. More Info: $_"            
}
exit

标签: windowspowershellkey-bindings

解决方案


这种类型的任务(即创建一个关闭活动窗口的热键)看起来比 Powershell更适合AutoHotkey 。


例如,这可以在 AutoHotkey 中使用:

#d::WinClose A ;remaps Super+d to a "close the Active Window" command

如果您想了解有关如何设置AHK或此脚本特别是如何工作的更多信息,请随时 lmk。


推荐阅读