Search code examples
c#copy-pastepastekeystrokesimulate

Simulate keyboard stroke Ctrl+V using C# results "v" in target application


I've written a win app using c# that automatically contacts to other applications such as browsers. i want to copy something in my win app, and paste it into the browser without involving user to click or move mouse. I don't have any problem with simulating mouse cliks, but i have problem to simulating keyboard strokes. Copy (Ctrl+c) always works properly, but when i want to paste whatever in the clipboard, sometimes works properly and sometimes doesn't. Follwing is more about my problem: I want to stroke Ctrl+v using C# ,in the other words I want to paste something in the Clipboard to another application using C#.

I am using the following code:

SendKeys.Send("^{v}");

I tried the following code too:

SendKeys.Send("^(v)");

But the problem is that it results sometimes paste action and sometimes just stroke v key. Can anyone help with what is wrong with my application? and another question, is it the problem to access clipboard when other apps want to access it?


Solution

  • When using SendKeys.Send you need to use parentheses to hold multiple modifiers down. However, since you only have one modifier, you don't need any grouping and should only be sending "^v".

    The curly braces {} are used to specify a special key such as {ENTER}, a full list is available in the SendKey.Send docs

    According to the docs:

    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".