首页 > 解决方案 > Powershell【System.Windows.Forms.SendKeys】,发送shift+windowsKey+rightArrow组合

问题描述

在 powershell 中,我使用 [System.Windows.Forms.SendKeys] 发送击键。但是我在模拟 windows 键时遇到了麻烦。因为它不是我在文档列表中找到的键之一:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=netcore-3.1

在这篇文章中,我看到他们正在使用 ctrl+esc 模拟 Windows 键,但这似乎不起作用。

使用 SendKeys 发送 Windows 密钥

知道如何做到这一点吗?

标签: powershellsendkeys

解决方案


试试这个解决方案。它具有按下窗口键,以便您可以发送组合键。

**对我来说 Win + 右箭头键有效,但 shift 对我的机器没有任何影响。它可能对你有用。

$source = @"
using System;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeySends
{
    public class KeySend
    {
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        private const int KEYEVENTF_EXTENDEDKEY = 1;
        private const int KEYEVENTF_KEYUP = 2;
        public static void KeyDown(Keys vKey)
        {
            keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
        }
        public static void KeyUp(Keys vKey)
        {
            keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
        }
    }
}
"@
Add-Type -TypeDefinition $source -ReferencedAssemblies "System.Windows.Forms"
Function WinKey ($Key)
{
    [KeySends.KeySend]::KeyDown("LWin")
    [KeySends.KeySend]::KeyDown("Shift")
    [KeySends.KeySend]::KeyDown("$Key")
    [KeySends.KeySend]::KeyUp("LWin")
    [KeySends.KeySend]::KeyUp("Shift")
}

WinKey({Right})

推荐阅读