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

安稳与你 提交于 2019-12-21 12:23:45

问题


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.


回答1:


Code for modifying keys

alt = % , shift = + and ctrl = ^

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 ctrl shift F1 you will do

^+{F1}




回答2:


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



回答3:


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

textbox1.Paste();



回答4:


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



来源:https://stackoverflow.com/questions/15621147/how-simulate-ctrlv-keystrokes-paste-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!