Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell [System.Windows.Forms.SendKeys], send shift+windowsKey+rightArrow combination

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?

like image 204
Willem van der Veen Avatar asked Oct 24 '25 01:10

Willem van der Veen


1 Answers

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

like image 171
Kundan Avatar answered Oct 26 '25 18:10

Kundan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!