5

How to simulate a Ctrl-A + Ctrl-C using 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!

jith10
  • 503
  • 2
  • 11
  • 17
  • 3
    what is it that you *actually* want to do? – Default Jan 18 '13 at 08:58
  • I am simulating a ctrl a + ctrl c on a webbrowser form to copy the entire contents on clipboard. i used this SendKeys.SendWait(^a^c) but it is not copying the entire contents! – jith10 Jan 18 '13 at 08:59
  • If you "own" the webbrowser form, don't you have that value stored in some property/field anyways? Could you give an exact example of what you want to do? That will generate much better answers to your question. – Default Jan 18 '13 at 09:00
  • http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript – Jan Van Herck Jan 18 '13 at 09:01
  • You mean i should explore throught hte document property ? Ok i display a collective lab order details with results and other stuff in that webbrowseform. i am providing 2 buttons to the user. One is Copy which when clicked copies the entire contents to the clipboard. the other is Copy Selected which is to copy the selected contents or text using mouse cursor to the clipboard. – jith10 Jan 18 '13 at 09:03
  • Hi Jan Van Herck actually i am using a WebBrowserForm Control in the Windows Forms. If i am not wrong i cant use java scripting there or can i ? – jith10 Jan 18 '13 at 09:06
  • See the answer to my question, which should help you. http://stackoverflow.com/questions/7307868/c-sharp-simulate-key-press – Bali C Jan 18 '13 at 09:11
  • public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag public const int VK_RCONTROL = 0xA3; //Right Controlkey public const int KEYC_PRESS = 0x43; //KEY C keybd_event(VK_RCONTROL, 0, KEYEVENTF_EXTENDEDKEY, 0); keybd_event(KEYC_PRESS, 0, KEYEVENTF_EXTENDEDKEY, 0); keybd_event(KEYC_PRESS, 0, KEYEVENTF_KEYUP, 0); keybd_event(VK_RCONTROL, 0, KEYEVENTF_KEYUP, 0); Hi @Bali C i tried this but not happening.. is this right.. please verify – jith10 Jan 18 '13 at 09:41
  • @jith10 - whoops, you're right. Sorry about that, I'd never heard of "webbrowser forms" before and as a web developer I just assumed you were making an ASP.NET WebControls page :) – Jan Van Herck Jan 18 '13 at 09:42
  • @jith10 You will need to replace VK_RCONTROL with whatever left control is, and do the same for A and C. If you check the link in the answer it should tell you the hex code for the right keys. If not just give me a few minutes and I will post an answer. – Bali C Jan 18 '13 at 09:51
  • Thank you @Bali C... i will make those changes.. if in case i don't get it i will be waiting for your answer.. Thanks a ton!! – jith10 Jan 18 '13 at 09:53
  • 1) Update your question. Don't post your code in comments. 2) I think there is a good chance that you don't want to send key-presses in the first place. – CodesInChaos Jan 18 '13 at 10:00
  • @jith10 I have added my answer, just to clarify what it should look like :) – Bali C Jan 18 '13 at 10:11

3 Answers3

12

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);
}
Gerard
  • 11,799
  • 12
  • 65
  • 119
Bali C
  • 28,049
  • 34
  • 109
  • 147
  • Hi Bali thanks a lot for your post. I guess in any other case this would have worked. But in my case the browser control is only considering the first logical ‘grouping’ of html… maybe its hitting the next ‘anchor’ and that is causing it not to copy all...!!! Got to look in depth.. thanks a ton!! I will keep this post updated as and when i get a solution... – jith10 Jan 21 '13 at 06:03
  • 2
    Note that KEYEVENTF_EXTENDEDKEY is not key down. There is no define for key down. Use 0 (or make your own define). – GravityWell Dec 16 '14 at 16:57
  • `keybd_event(C, 0, 0, 0); keybd_event(C, 0, 2, 0);` works for me. – Gerard Nov 14 '19 at 08:28
0

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

myselfesteem
  • 673
  • 1
  • 6
  • 20
0

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/

Markus
  • 392
  • 2
  • 6