Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Windows key using SendKeys

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.

like image 725
nuriaycan Avatar asked Apr 28 '12 18:04

nuriaycan


People also ask

How do you send in SendKeys?

SendWait("~"); // How to press enter? SendKeys. SendWait("{ENTER}"); // How to press enter? You link to Send(), but tell us to use SendWait().

How do I use SendKeys SendWait?

Use SendWait to send keystrokes or combinations of keystrokes to the active application and wait for the keystroke messages to be processed. You can use this method to send keystrokes to an application and wait for any processes that are started by the keystrokes to be completed.

How do you press a key in C#?

ReadKey() Method in C# Console. ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user.


2 Answers

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.

like image 137
yamen Avatar answered Sep 20 '22 02:09

yamen


Alt+F4 is working only in brackets

SendKeys.SendWait("(%{F4})"); 
like image 44
Oleg Gavril Avatar answered Sep 19 '22 02:09

Oleg Gavril