【问题标题】:How to handle multiple key press simultaneously in powershell? Eg:Windows logo key + Alt + PrtScn:如何在PowerShell中同时处理多个按键?例如:Windows 徽标键 + Alt + PrtScn:
【发布时间】:2020-07-08 11:07:58
【问题描述】:

在 powershell ISE 中尝试了以下代码,但它只接受第一个键(按 Win),之后它接受下一个键为“g”。

[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.SendKeys]::sendwait("(^{ESCAPE})g")

但我想一次按 Win+g 来打开一些应用程序,例如 xbox 游戏栏。

有人可以指导我吗?

【问题讨论】:

    标签: c# powershell automation powershell-ise pester


    【解决方案1】:

    无法使用本机 SendWait 方法完成,但我们可以使用 WinAPI 来执行此操作,如下所示https://social.msdn.microsoft.com/Forums/vstudio/en-US/f2d88949-2de7-451a-be47-a7372ce457ff/send-windows-key?forum=csharpgeneral

    $code = @'
    namespace SendTheKeys {
      class SendIt {
       public static void Main(string[] args) {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
            private 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);
            }
        KeyDown(Keys.LWin);
        KeyDown(Keys.G);
        KeyUp(Keys.LWin);
        KeyUp(Keys.G);
      }
     }
    }
    '@
    Add-Type -TypeDefinition $code -Language CSharp
    [SendTheKeys.SendIt]::Main()
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多