C# 如何使用 keybd_event 模拟 Ctrl A + Ctrl C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14395377/
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 to simulate a Ctrl A + Ctrl C using keybd_event
提问by jith10
How to simulate a Ctrl-A+ Ctrl-Cusing keybd_event
?
如何模拟Ctrl- A+ Ctrl-C使用keybd_event
?
Because I am simulating a ctrl a + ctrl c on a webbrowser form to copy the entire contents on clipboard. i used the SendKeys.SendWait but it is not copying the entire contents!
因为我在 webbrowser 表单上模拟 ctrl a + ctrl c 来复制剪贴板上的全部内容。我使用了 SendKeys.SendWait 但它没有复制整个内容!
回答by Bali C
This should work
这应该工作
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const int KEYEVENTF_KEYDOWN = 0x0000; // New definition
public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
public const int VK_LCONTROL = 0xA2; //Left Control key code
public const int A = 0x41; //A key code
public const int C = 0x43; //C key code
public static void PressKeys()
{
// Hold Control down and press A
keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(A, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(A, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);
// Hold Control down and press C
keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(C, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(C, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);
}
回答by myselfesteem
You are able to fire the Cntrl-A + Cntrl-C event, am I right? But for some reason you are not copying all the webpage text to clipboard?
你可以触发 Cntrl-A + Cntrl-C 事件,对吗?但是出于某种原因,您没有将所有网页文本复制到剪贴板?
I don't know much about doing a Cntrl-A + Cntrl-C event, and I'm also not clear as to what u r trying to do, but I gave it my best shot and came up with something that grabs all the text from a webpage and copies it to the clipboard off of a button click event...(now obviously you would want to use ur Cntrl-A + Cntrl-C). Also for debugging purposes I put the clipboard text in a .txt file so you can double check.
我对做 Cntrl-A + Cntrl-C 事件不太了解,我也不清楚你想做什么,但我尽力了,想出了一些能抓住所有文字的东西从网页并通过按钮单击事件将其复制到剪贴板......(现在显然你想使用你的 Cntrl-A + Cntrl-C)。同样出于调试目的,我将剪贴板文本放在 .txt 文件中,以便您可以仔细检查。
I'm also using the HTML Agility Pack. You can get that from http://htmlagilitypack.codeplex.com/
我也在使用 HTML Agility Pack。你可以从http://htmlagilitypack.codeplex.com/得到它
CODE
代码
private void btnClip_Click(object sender, EventArgs e)
{
string address = "http://animalrights.about.com/";
string text = "";
// Retrieve resource as a stream
Stream data = client.OpenRead(new Uri(address)); //client here is a WebClient
//create document
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.Load(data);
//receive all the text fields
foreach (HtmlNode node in document.DocumentNode.SelectNodes("//child::p"))
{
text += node.InnerText + "\n\n";
}
Clipboard.SetText(text);
string path = @"C:\Users\David\Documents\Visual Studio 2012\Projects\CopyToClipBoard\CopyToClipBoard\bin\MyTest.txt";
// Delete the file if it exists.
if (File.Exists(path))
{
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path, 1024))
{
Byte[] info = new UTF8Encoding(true).GetBytes(text);
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
//destroy data object
data.Close();
data.Dispose();
}
Open notepad to check file
打开记事本查看文件
回答by Markus
Windows Input Simulator makes this super easy.
Windows 输入模拟器使这变得非常简单。
The Windows Input Simulator provides a simple .NET (C#) interface to simulate Keyboard or Mouse input using the Win32 SendInput method. All of the Interop is done for you and there's a simple programming model for sending multiple keystrokes.
Windows 输入模拟器提供了一个简单的 .NET (C#) 接口来使用 Win32 SendInput 方法模拟键盘或鼠标输入。所有的互操作都为您完成,并且有一个简单的编程模型用于发送多个击键。
Nuget package -> Install-Package InputSimulator
Nuget 包 -> 安装包 InputSimulator