41

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.

Andrea
  • 11,801
  • 17
  • 65
  • 72
nuriaycan
  • 413
  • 1
  • 4
  • 5

5 Answers5

30

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.

yamen
  • 15,390
  • 3
  • 42
  • 52
9

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);
Ali Rasouli
  • 1,705
  • 18
  • 25
  • Any idea how to use this in PowerShell , via Add-Type ? – Rakha May 21 '19 at 17:20
  • please see this page:https://github.com/TChatzigiannakis/InputSimulatorPlus/blob/master/WindowsInput.Tests/InputSimulatorExamples.cs you can run run command and then in run type any app name and then do every thing you want. – Ali Rasouli May 22 '19 at 19:30
  • 1
    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); – Ali Rasouli May 22 '19 at 19:34
6

Alt+F4 is working only in brackets

SendKeys.SendWait("(%{F4})");
Oleg Gavril
  • 71
  • 1
  • 2
2
SetForegroundWindow( /* window to gain focus */ );
SendKeys.SendWait("^{ESC}"); // ^{ESC} is code for ctrl + esc which mimics the windows key.
m-jo
  • 46
  • 4
0

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

supwat
  • 11
  • 3