Sending Windows Key Using Sendkeys

Sending Windows key using SendKeys

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.

SendKeys.Send and Windows Key

CTRL+ESC does not simulate the WIN key, it just calls the start menu.

A bit of P/Invoke always makes everyone happy:

using System.Runtime.InteropServices;
using System.Windows.Forms;

static class KeyboardSend
{
[DllImport("user32.dll")]
private 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);
}
}

And you call it this way :

KeyboardSend.KeyDown(Keys.LWin);
KeyboardSend.KeyDown(Keys.D4);
KeyboardSend.KeyUp(Keys.LWin);
KeyboardSend.KeyUp(Keys.D4);

Tested, it works. Have fun!

How do I use the sendkeys.send to work with the windows key


Here is a workaround that you can use the key combination Ctrl + Esc to replace Windows Key. The following is a simple demo.

private void btnSend_Click(object sender, EventArgs e)
{
SendKeys.Send("^{ESC}");
}

Hope this can help you.


Modified:

If you want to lock the PC via code, you can call LockWorkStation function.

[DllImport("user32 ")]
public static extern bool LockWorkStation();

private void button1_Click(object sender, EventArgs e)
{
LockWorkStation();
}

Besides, if you want achieve Win+D, Win+E, etc., you can try the following code.

[DllImport("User32.dll")]
public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);

private void button1_Click(object sender, EventArgs e)
{
keybd_event(0x5b, 0, 0, 0);
keybd_event(68, 0, 0, 0); // D is 68, and E is 69
keybd_event(0x5b, 0, 0x2, 0);
keybd_event(68, 0, 0x2, 0);
}

Send Windows Key in batch script

There is currently no way to simulate the windows home logo in sendkey's, howevery this does not mean it's not possible.

If you take a look at the windows shortcut keys you will find you can simulate Open Start with the following key combinations: Ctrl + Esc.

To simulate this in batch, you can use: powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^{ESCAPE}') or in your case: %SendKeys% "^{ESCAPE}".

As stated in sendkeys:

  • "^" - Simulates a Ctrl key press.
  • "{ESCAPE}" - Simulates a Esc key press.

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

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

Send keys syntax needed

the BluePrism uses standard C# implementation of sendkeys. If you'd like to read more sendkeys class, then you can find the information on Microsoft Developer Network.

The standard implementation does not allow using windows key, so to do that you'll need to work around that with BluePrism code stages, and write a code in one of BluePrism supported coding languages (c#, j#, vb.net)

That question how to do that was answered several times already on stackoverflow. Some solutions are available on these pages: Link 1, Link 2, Link 3



Related Topics



Leave a reply



Submit