简体   繁体   English

如何使用 C# 模拟 CTRL+V 击键(粘贴)

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

How can we simulate CTRL + V keys (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.我有一个没有用于访问的 id 的文本框,例如textbox1.Text = someValue在这里不起作用。
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 .由于某些原因,我们确实需要模拟CTRL + V ,这意味着我们不能使用像inputsimulator这样的外部库。

Character vs key字符与键

% => alt , + => shift and ^ for to send ctrl key % => 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}" .模拟单个修饰键与另一个键的解释如下Step1:聚焦文本框,您要在其上执行两个键,然后Step2:发送键,例如 control-v 将像"^{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更新:对于发送三个键(两个修改键和其他键),如实现ctrl shift F1您将发送以下

^+{F1}

Microsoft Docs Ref Microsoft 文档参考

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

此函数已内置: TextBoxBase.Paste()

textbox1.Paste();

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM