如何使用keybd_event模拟Ctrl a+Ctrl C

本文关键字:Ctrl a+Ctrl 模拟 event 何使用 keybd | 更新日期: 2023-09-27 17:54:36

如何使用keybd_event模拟Ctrl-a+Ctrl-C

因为我在网络浏览器表单上模拟ctrl a+ctrl c来复制剪贴板上的全部内容。我使用了SendKeys.SendWait,但它没有复制全部内容!

如何使用keybd_event模拟Ctrl a+Ctrl C

这应该能在中工作

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

您可以触发Cntrl-A+Cntrl-C事件,对吗?但出于某种原因,您没有将所有网页文本复制到剪贴板?

我对Cntrl-a+Cntrl-C事件不太了解,也不清楚你想做什么,但我尽了最大努力,想出了一个办法,可以从网页上抓取所有文本,并从按钮点击事件中将其复制到剪贴板。。。(现在很明显,您希望使用ur Cntrl-A+Cntrl-C(。出于调试目的,我将剪贴板文本放在.txt文件中,这样您就可以进行双重检查。

我也在使用HTML敏捷包。你可以从http://htmlagilitypack.codeplex.com/

代码

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

打开记事本检查文件

Windows输入模拟器让这变得非常容易。

Windows输入模拟器为使用Win32 SendInput方法模拟键盘或鼠标输入。全部的Interop已经为您完成,并且有一个简单的编程模型用于发送多个击键。

Nuget软件包->安装软件包InputSimulator

https://inputsimulator.codeplex.com/