3

In powershell I'm using [System.Windows.Forms.SendKeys] to send keystrokes. However I'm having trouble emulating the windows key. Since it isn't one of the keys I found in the list in the docs:

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

In this post I saw that they were emulating the windows keys using ctrl+esc however this doesn't seem to work.

Sending Windows key using SendKeys

Any idea how to accomplish this?

Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155

1 Answers1

9

Try this solution. It has press down window key so that you can send combination key along with that.

**for me Win + Right Arrow key worked but shift doesn't have any effect on my machine. It might work for you.

$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("ShiftKey")
    [KeySends.KeySend]::KeyDown("$Key")
    [KeySends.KeySend]::KeyUp("LWin")
    [KeySends.KeySend]::KeyUp("ShiftKey")
}

WinKey({Right})

.NET System.Windows.Forms Keys list

Paul π
  • 133
  • 11
Kundan
  • 1,394
  • 1
  • 13
  • 26