Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 392
SilverLight Avatar asked Mar 25 '13 17:03

SilverLight


3 Answers

Character vs key

% => alt , + => shift and ^ for to send 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

like image 119
Sami Avatar answered Nov 11 '22 23:11

Sami


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 ();
}
like image 40
rut0.wut Avatar answered Nov 12 '22 01:11

rut0.wut


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

textbox1.Paste();
like image 1
Breeze Avatar answered Nov 11 '22 23:11

Breeze