如何使用 C# 模拟 CTRL+V 击键(粘贴)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15621147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How simulate CTRL+V keystrokes (paste) using C#
提问by SilverLight
How can we simulate CTRL+Vkeys (paste) using C#?
我们如何使用 C#模拟CTRL+V键(粘贴)?
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.
我有一个没有用于访问的 id 的文本框,例如textbox1.Text = someValue
在这里不起作用。
我想通过单击它来填充该文本框(从剪贴板)。由于某些原因,我们确实需要模拟CTRL+ V,这意味着我们不能使用像inputsimulator这样的外部库。
采纳答案by Sami
Code for modifying keys (See update below for more than two keys)
修改密钥的代码(两个以上的密钥见下面的更新)
alt = %
, shift = +
and ctrl = ^
alt = %
,shift = +
和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
模拟单个修饰键与另一个键的说明如下
步骤 1:聚焦文本框,您要在其上执行两个键,然后步骤 2:发送键,例如 control-v 将像 一样发送"^{v}"
。这是代码
target_textBox.Focus();
SendKeys.Send("^{v}");
target_textBox.Focus();
is needed only when target textbox is not focused at the time of sending key
target_textBox.Focus();
仅当发送密钥时目标文本框未聚焦时才需要
Update:
更新:
For sending three keys (two modifying keys plus other key) like ctrl shift F1
you will do
像ctrl shift F1
您一样发送三个密钥(两个修改密钥和其他密钥)
^+{F1}
^+{F1}
回答by rut0.wut
Why don't you override the TextBox OnClick event than when the event is called, set the Text property to Clipboard.GetText()
为什么不覆盖 TextBox OnClick 事件而不是在调用事件时将 Text 属性设置为 Clipboard.GetText()
Like:
喜欢:
private void textBox1_Click ( object sender, EventArgs e )
{
textBox1.Text = Clipboard.GetText ();
}
回答by Breeze
回答by Александр Бурундук
some JS do not permit to change value in usual way
一些 JS 不允许以通常的方式更改值
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}"
//但不是“^{v}”