48

I am working on shortcuts in C#. I succeed implementing Ctrl, Alt and Shift with SendKeys.

Like this;

Ctrl + C:

System.Windows.Forms.SendKeys.SendWait("^c");

or Alt + F4:

System.Windows.Forms.SendKeys.SendWait("%{F4}");

But I can't send "Windows Key" with SendKeys. I tried ex: Win + E : .SendWait("#e") but it's not working. What should I use instead of "#"?

Thanks.

6
  • 19
    Windows key is actually equivalent of a CTRL + ESC, have you tried with that? Commented Apr 28, 2012 at 18:12
  • 2
    Seconded. It is SendSystemKeys "{Ctrl+Esc}" Commented Apr 28, 2012 at 18:15
  • 2
    Yes, I tried that with .SendWait("^{ESC}") but that's not working too. Is there any wrong code in this syntax? Commented Apr 28, 2012 at 19:20
  • 1
    Process.Start("explorer.exe"); Commented Apr 28, 2012 at 19:20
  • 7
    Win + E , is just a sample. Imagine that, I will need Win + R (Run) or Win + L. Commented Apr 28, 2012 at 19:28

6 Answers 6

31

OK turns out what you really want is this: http://inputsimulator.codeplex.com/

Which has done all the hard work of exposing the Win32 SendInput methods to C#. This allows you to directly send the windows key. This is tested and works:

InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.VK_E);

Note however that in some cases you want to specifically send the key to the application (such as ALT+F4), in which case use the Form library method. In others, you want to send it to the OS in general, use the above.


Old

Keeping this here for reference, it will not work in all operating systems, and will not always behave how you want. Note that you're trying to send these key strokes to the app, and the OS usually intercepts them early. In the case of Windows 7 and Vista, too early (before the E is sent).

SendWait("^({ESC}E)") or Send("^({ESC}E)")

Note from here: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

Note that since you want ESC and (say) E pressed at the same time, you need to enclose them in brackets.

Sign up to request clarification or add additional context in comments.

5 Comments

Cool, your gravatar is almost identical to the OPs. :-P
Here is what I really want it. Many thanks for your reply, very grateful!
input simulator requires you to reference the Dll in excel right?
How to send the key for one specific window? For example, i want to send the Winkey to an aplication that's in full screen and its active. It isn't working the way i need. Thank you!
10

download InputSimulator from nuget package.

then write this:

        var simu = new InputSimulator();
        simu.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.VK_E);

in my case to create new vertial desktop, 3 keys needed and code like this(windows key + ctrl + D):

        simu.Keyboard.ModifiedKeyStroke(new[] { VirtualKeyCode.LWIN, VirtualKeyCode.CONTROL }, VirtualKeyCode.VK_D);

3 Comments

Any idea how to use this in PowerShell , via Add-Type ?
please see this page:github.com/TChatzigiannakis/InputSimulatorPlus/blob/master/… you can run run command and then in run type any app name and then do every thing you want.
for example: var sim = new InputSimulator(); sim.Keyboard .ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.VK_R) .Sleep(1000) .TextEntry("notepad") .Sleep(1000) .KeyPress(VirtualKeyCode.RETURN) .Sleep(1000) .TextEntry("These...") .TextEntry("and in 5 seconds.") .Sleep(5000) .ModifiedKeyStroke(VirtualKeyCode.MENU, VirtualKeyCode.SPACE) .KeyPress(VirtualKeyCode.DOWN) .KeyPress(VirtualKeyCode.RETURN);
7

Alt+F4 is working only in brackets

SendKeys.SendWait("(%{F4})");

1 Comment

Thanks works perfect. Discovery: If you want to send alt+letter. If I use lower case letter (ex alt+w, "(%{w})"), it works fine. But if I use uppercase letter (ex alt+W) windows forms consumes the Alt to select the form menustrip first-item and the key combo fails. This is on .Net 4.8
2
SetForegroundWindow( /* window to gain focus */ );
SendKeys.SendWait("^{ESC}"); // ^{ESC} is code for ctrl + esc which mimics the windows key.

Comments

0

For sending combination of Ctrl+Alt+Right ==> use this ==> "(^%({RIGHT}))"

Comments

0

Here is a C# solution. I can simulate the pressing of Ctrl, LWin and Left arrow (switching to virtual desktop on the left), for example, but you can test and adapt to your needs.

Create a new Winforms application, add a button to it (I did not rename it here), and paste this inside the code of the form. Then run the app and click on the button.

using System.Runtime.InteropServices;

namespace SendWinKey
{
    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);
        }

//see https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=windowsdesktop-9.0 for key names
        public static void WinKey(Keys key)
        {
            KeySend.KeyDown(Keys.LWin);
            KeySend.KeyDown(key);
            KeySend.KeyUp(Keys.LWin);
            KeySend.KeyUp(key);
        }
        public static void ControlWinKey(Keys key)
        {
            KeySend.KeyDown(Keys.LWin);
            KeySend.KeyDown(Keys.ControlKey);
            KeySend.KeyDown(key);
            KeySend.KeyUp(Keys.LWin);
            KeySend.KeyUp(Keys.ControlKey);
            KeySend.KeyUp(key);
        }
        public static void ShiftWinKey(Keys key)
        {
            KeySend.KeyDown(Keys.LWin);
            KeySend.KeyDown(Keys.ShiftKey);
            KeySend.KeyDown(key);
            KeySend.KeyUp(Keys.LWin);
            KeySend.KeyUp(Keys.ShiftKey);
            KeySend.KeyUp(key);
        }
        public static void AltWinKey(Keys key)
        {
            KeySend.KeyDown(Keys.LWin);
            KeySend.KeyDown(Keys.Menu);
            KeySend.KeyDown(key);
            KeySend.KeyUp(Keys.LWin);
            KeySend.KeyUp(Keys.Menu);
            KeySend.KeyUp(key);
        }
        public static void ControlShiftWinKey(Keys key)
        {
            KeySend.KeyDown(Keys.LWin);
            KeySend.KeyDown(Keys.ControlKey);
            KeySend.KeyDown(Keys.ShiftKey);
            KeySend.KeyDown(key);
            KeySend.KeyUp(Keys.LWin);
            KeySend.KeyUp(Keys.ShiftKey);
            KeySend.KeyUp(Keys.ControlKey);
            KeySend.KeyUp(key);
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            KeySend.ControlWinKey(Keys.Left);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.