12

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.

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • More information needed - are you able to subclass the textbox? Are you in Silverlight, WinForms or WPF? Why are you unable to gain a reference to the textbox? – Alex Mar 25 '13 at 17:36
  • @Alex G i am working with webbrowser control in a winform and after navigate to an specific page i can not access goal textbox in webbrowser by it's id. – SilverLight Mar 25 '13 at 17:39
  • if your textbox is in the webbrowser this is not at all a winforms problem, it's all about HTML scripting – madd0 Mar 25 '13 at 17:40
  • 1
    How are you finding the textbox? Can you get a handle to the textbox window? That would be a better way to insert text than emulating keystrokes. – Jonathan Wood Mar 25 '13 at 17:46
  • i am clicking on that textbox by simulating mouse position and it's click. i really want to learn how can we simulate ctrl+v with c# language dear Jonathan Wood – SilverLight Mar 25 '13 at 17:49

5 Answers5

26

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

Sami
  • 8,168
  • 9
  • 66
  • 99
  • thanks for the answer dear Sami, what about shift+home and shift+end? – SilverLight Mar 25 '13 at 18:00
  • @MoonLight - Here's a link with a bunch of useful information about all the different combinations. http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.100).aspx – Jon Raynor Mar 25 '13 at 18:01
  • If you are talking about sending those keys then I think `"+{home}"` `"+{end}"` would work – Sami Mar 25 '13 at 18:02
  • 1
    Note: If you want to use this outside of a WinForm context, e.g. in LinqPad, you will need `SendKeys.SendWait("^{v}");` to make it work. – Matt Jan 25 '23 at 07:19
3

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 ();
}
rut0.wut
  • 381
  • 1
  • 3
  • 6
  • I wanted to vote you up, for I came to know a thing which I did not know, but i m sorry it is not working for me, have u tried it? – Sami Mar 25 '13 at 18:31
  • Have you added that to the TextBox's Click event? – rut0.wut Mar 25 '13 at 18:50
  • Sorry I missed the assignment and just used `Clipboard.GetText ();` but now its working with any event (not only for click) as it should.. – Sami Mar 25 '13 at 18:54
1

This function is already built in: TextBoxBase.Paste()

textbox1.Paste();
Breeze
  • 2,010
  • 2
  • 32
  • 43
0

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

0

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}");
Dinariys
  • 11
  • 3