How to simulate a Ctrl A + Ctrl C using keybd_event

前端 未结 3 2177
耶瑟儿~
耶瑟儿~ 2020-12-06 14:17

How to simulate a Ctrl-A + Ctrl-C using keybd_event?

Because I am simulating a ctrl a + ctrl c on a webbrows

相关标签:
3条回答
  • 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?

    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.

    I'm also using the HTML Agility Pack. You can get that from 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

    0 讨论(0)
  • 2020-12-06 15:10

    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);
    }
    
    0 讨论(0)
  • 2020-12-06 15:14

    Windows Input Simulator makes this super easy.

    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.

    Nuget package -> Install-Package InputSimulator

    https://inputsimulator.codeplex.com/

    0 讨论(0)
提交回复
热议问题