2

Tried the below code in powershell ISE but it accept only 1st key (Pressing Win) after that it accepts next key as 'g'.

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

But I want to Press the Win+g at a time to open some applications like xbox game bar.

Can anyone guide me on this?

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
GKA
  • 23
  • 2

1 Answers1

0

It can't be done with native SendWait method but we can use WinAPI to do this as shown here 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()
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Could you please check the question on the below link https://stackoverflow.com/questions/60946380/multiple-key-press-simultaneously-for-windows-logo-key-alt-prtscn-in-powersh – GKA Mar 31 '20 at 08:33