You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell SendKeys发送Win+向上箭头快捷键放大窗口失效如何解决

问题原因
  • 你编写的快捷键字符串完全不符合Win+向上箭头的组合规则:System.Windows.Forms.SendKeys本身没有内置的Win键转义符,你写的"+(^{ESC}{UP})"实际对应发送的是Shift+Ctrl+Esc后按向上键,和你要实现的Win+向上箭头效果完全无关。
  • 模拟按键的逻辑没有和当前活动窗口做绑定,若脚本运行时窗口焦点被抢占,按键会发送到错误的目标。
  • 存在UAC权限拦截:如果要放大的窗口是高权限运行的,你的PowerShell没有以管理员权限启动的话,按键事件会被系统直接拦截。
修复方案

方案1(推荐):直接调用Win32 API最大化活动窗口

该方案不依赖模拟按键,直接操作窗口属性,成功率100%,不受焦点、快捷键冲突影响:

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
    [DllImport("user32.dll")]
    public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();
}
"@
# 3对应窗口最大化常量SW_MAXIMIZE
[Win32]::ShowWindowAsync([Win32]::GetForegroundWindow(), 3)

方案2:模拟Win+向上箭头按键(仅当你需要触发系统窗口分栏逻辑时使用)

因为SendKeys不支持Win键,改用Win32的keybd_eventAPI实现按键模拟:

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
    [DllImport("user32.dll", SetLastError = true)]
    public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
    public const int KEYEVENTF_KEYUP = 0x0002;
    public const int VK_LWIN = 0x5B;
    public const int VK_UP = 0x26;
}
"@
# 按下左Win键
[Win32]::keybd_event([Win32]::VK_LWIN, 0, 0, [UIntPtr]::Zero)
# 按下向上键
[Win32]::keybd_event([Win32]::VK_UP, 0, 0, [UIntPtr]::Zero)
# 松开向上键
[Win32]::keybd_event([Win32]::VK_UP, 0, [Win32]::KEYEVENTF_KEYUP, [UIntPtr]::Zero)
# 松开左Win键
[Win32]::keybd_event([Win32]::VK_LWIN, 0, [Win32]::KEYEVENTF_KEYUP, [UIntPtr]::Zero)

注意事项

  • 运行上述任意脚本时,如果目标窗口是管理员权限运行的,PowerShell也需要以管理员权限启动,否则操作会被系统拦截。

内容的提问来源于stack exchange,提问作者Abdelkrim Balaboula

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 18:42:02