Sending Windows key using SendKeys - c#

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.

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.

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);

Alt+F4 is working only in brackets
SendKeys.SendWait("(%{F4})");

SetForegroundWindow( /* window to gain focus */ );
SendKeys.SendWait("^{ESC}"); // ^{ESC} is code for ctrl + esc which mimics the windows key.

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

Related

c# SendKeys.SendWait function, send numpad digits

so I was working on an application which supposed executes digit keys for another application which isn't made by me, this application only supports numpad digits and it doesn't support the digits below F1-12s so whenever I execute "{number}" or just "number" ex:
SendKeys.SendWait("{1}");
or
SendKeys.SendWait("1");
just simple nothing happens, any one got idea on how to send number as numpad digit?
I've found my answer on this topic
How to simulate a Ctrl A + Ctrl C using keybd_event
here's the area to find keys:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

Translating Windows.forms.Keys to real local keyboard value

I use the namespace Windows.Forms.Keys
I would like to be able to catch and use some special like characters like é,è,à,ç, but when the program fire the event KeyDown, the KeyEventArg just return me the value "D1" to "D9".
What could I do to get the real char associated to these keys ?
Short answer: use KeyPress instead of KeyDown.
KeyDown is really designed to work with the physical layout of the keyboard (well, the logical layout of the physica... forget it :D). This is very different from the character that given physical key represents.
On the other hand KeyPress is all about characters being input from the keyboard, rather than keys being pressed, really. Note how KeyPress supports features like AltGr + someKey and char repetition etc.
If you really need to use KeyDown/KeyUp, you'll have to emulate the way windows keyboard system works to determine the char to output (for example, if you're making a keyboard mapping screen for a game or something like that). You can use the ToAscii WinAPI method (https://msdn.microsoft.com/en-us/library/ms646316.aspx).
Apart from that, you still have to understand the meaning of the key combinations - for example, on my keyboard, if I press 1, I get +. If I press Shift+1, I get 1. If I press AltGr + 1, I get !. Which of those do you care about? Maybe Shift + 1 should be interpreted as 1 (what KeyPress does). Maybe it should be interpreted as Shift + 1 (the easiest, you already have that). And maybe it should be interpreted as Shift + +, the way it's usually used for hotkey bindings or keyboard mappings in games.
It should be pretty obvious by now that this is actually far from trivial. You need some mechanism to interpret the "raw" input data - and different interpretations make different sense for different initial conditions. You're basically asking for a mixed approach between the two obvious options - you're mixing virtual keys and "real" characters.

C# Sendkeys class {SHIFT}

I am trying to use the Sendkeys class to send a SHIFT key. But it doesn't allow for SHIFT. Only BACKSPACE or ENTER or basically anything but SHIFT. I need a way to send shift keys, like, Sendkeys.Send({SHIFT});
Is there a way to do this? Is there another way to send SHIFTs?
%() sends just the Alt command. So I assume that +() will send just the shift key. However I cannot think of a way to test this, since the shift key does nothing by itself. pressing shift 5 times normally prompts if you want to turn on sticky keys, but +()+()+()+()+() does not prompt for sticky keys.
Read the senction just after it explains how to send F1 to F12: https://msdn.microsoft.com/en-us/library/office/gg278655.aspx
%A = Alt+A
%(AE) = Alt+A+E
%()AE = ALT,A,E

SendInput function handled in notepad/wordpad as if CTRL modifier is down

BACKGROUND:
I am writing a 32 bit WPF (C#) application which functions as an on-screen keyboard. It publishes the selected key strokes to the focused window as if physical keys had been pressed, exactly as the Microsoft On-Screen Keyboard, OSK.exe behaves.
PROBLEM:
I was using the InputSimulator library with some success (code here: InputSimulator class which builds the INPUT array), but found that certain key strokes were not being recognised by Notepad as expected, e.g. the arrow keys were behaving as if CTRL was being held down. Similarly, the WIN key was not working as expected, which could also be explained if Windows was treating the input as Ctrl+Win.
ATTEMPTED SOLUTION:
I ported the InputSimulator source into my project and made some modifications to how keystrokes are sent to SendInput, based on the calls to SendInput that OSK.exe sends (captured using API Monitor). The key differences I observed (and replicated in my code) for a KeyDown/KeyUp are:
InputSimulator passes a Keycode, and an ExtendedKey flag (if the key is extended), plus the KeyUp flag when releasing the key.
OSK adds the key's Scancode, and the ScanCode flag for the majority of keys.
OSK also has some other differences; individual keys where the KeyCode is not passed at all, where the ScanCode is not passed at all, and differences in which keys require the ExtendedKey flag.
The result of my changing the code to replicate how OSK calls SendInput was that even more keys now behave as if CTRL is being detected by my target/focussed application (typically Notepad or Wordpad). However, from direct comparison of my app and OSK in API Monitor I believe that my calls to SendInput are identical to OSK's calls.
N.B. OSK works flawlessly on my Windows 8.1 (64 bit) laptop.
ISOLATED PROBLEM SPACE:
To minimise the problem space I simulated a single key down/key up combination of the 'S' key from my application on a newly restarted PC (so that I can be sure the key down states were not contaminated from previous runs or physical key strokes). The target was Notepad and then WordPad - both reacted by opening the 'Save As' dialog, suggesting that they had interpreted my keystrokes as CTRL+S. API Monitor detected only 2 calls to SendInput (the KeyDown and then KeyUp), and these calls matched the same experiment using OSK. Here is the log;
2014-12-10 21:29:54,650 Calling native method SendInput with params:
nInputs:1
pInputs[0]:
Type:1(Keyboard)
Data:
MOUSEINPUT:
X:2031699
Y:8
MouseData:0
Flags:0 ()
Time:0
KEYBDINPUT:
KeyCode:83(VK_S)
Scan:31
Flags:8 (KEYEVENTF_SCANCODE)
Time:0
ExtraInfo:0
HARDWAREINPUT:
Msg:2031699
ParamL:8
ParamH:0
cbSize:28
2014-12-10 21:29:54,651 Calling native method SendInput with params:
nInputs:1
pInputs[0]:
Type:1(Keyboard)
Data:
MOUSEINPUT:
X:2031699
Y:10
MouseData:0
Flags:0 ()
Time:0
KEYBDINPUT:
KeyCode:83(VK_S)
Scan:31
Flags:10 (KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE)
Time:0
ExtraInfo:0
HARDWAREINPUT:
Msg:2031699
ParamL:10
ParamH:0
cbSize:28
The only noticeable difference is that OSK passes the cbSize param as 40, which I cannot fake (the call fails if I manually pass 40). My size is 28, which I get by passing the below. I have no idea why the sizes differ as my structure definitions match the MSDN documentation (I have not modified these from the original InputSimulator code).
var cbSize = Marshal.SizeOf(typeof (INPUT));
OTHER ATTEMPTED FIXES:
I have tried leaving the VirtualCode param blank (0) when a ScanCode (and ScanCode flag) are specified, but this does not change the result. Idea from here: SO question
I have tried adding a trailing call to keybd_event, but this does not change the result. Idea from here: MSDN thread
(keybd_event(0x41, 0, 0, 0);)
I have tried adding a thread sleep between each call to SendInput, i.e. a delay between the KeyDown and KeyUp calls.
I have modified my structs and winapi function definitions to match PINVOKE.net
I have recompiled my application as 64 bit (on my 64 bit machine) - this corrected the cbSize to 40, but did not change the behaviour.
Any help would be GREATLY appreciated. Also any suggestions regarding other debugging tools I could use? I have attempted to debug all Keyboard functions which OSK may be calling (to detect other calls to keybd_event, for example), but none are logged besides the calls to SendInput.
Right, so the issue was PEBCAK.
I missed the most obvious thing imaginable - the trigger signal that I have been using while testing (i.e. the signal to say that I want to press the currently focused an on-screen key) is the left CTRL key. I've been pressing the CTRL key and then wondering why Windows thinks the CTRL key is pressed. Shoot me now.
I've been using it for so long, and have been so focused on the details of the potentially misbehaving WinAPI calls that I missed the most obvious cause.
I will leave this here to remind myself to use Occam's Razor.
Some of the debugging journey above may be useful to someone; my altered version of the InputSimulator code works fine, as does the original, unaltered code.

How simulate CTRL+V keystrokes (paste) using C#

How can we simulate CTRL+V keys (paste) using C#?
I have a textbox that hasn't a id for access, for example textbox1.Text = someValue won't work here.
I want to fill that textbox (from clipboard) by clicking on it. For some reasons we exactly need simulate CTRL+V, mean we cannot use external libraries like inputsimulator.
Character vs key
% => alt , + => shift and ^ is used for ctrl key
Original Answer:
Simulation of single modifier key with another key is explained below
Step1: Focus the textBox, on which you want to perform two keys and then Step2: send the key for example control-v will be sent like "^{v}". Here is the code
target_textBox.Focus();
SendKeys.Send("^{v}");
target_textBox.Focus(); is needed only when target textbox is not focused at the time of sending key
Update: For sending three keys (two modifying keys plus other key) like to achieve ctrl shift F1 you will send following
^+{F1}
Microsoft Docs Ref
Why don't you override the TextBox OnClick event than when the event is called, set the Text property to Clipboard.GetText()
Like:
private void textBox1_Click ( object sender, EventArgs e )
{
textBox1.Text = Clipboard.GetText ();
}
This function is already built in: TextBoxBase.Paste()
textbox1.Paste();
some JS do not permit to change value in usual way
inputList[21].SetAttribute("value", txtEMail.Text);
you should try something like this:
inputElement.InvokeMember("focus");
inputElement.InvokeMember("click"); //sometimes helpfull
Clipboard.SetDataObject(txtEMail.Text);
SendKeys.Send("^(v)");
//but not "^{v}"
In case the language in the operating system is not English, this option may not work:
SendKeys.Send("^{v}");
Then try this option:
SendKeys.Send("+{INSERT}");

Categories