Insert text into the textbox of another application
Asked Answered
J

4

13

How do I, using C# or C++, insert text into the textbox of another application? I did this a long time ago and seemed to remember something about using the applications HWND. But since that change for every instance of the application I feel that I fon't remember the complete story. Do I somehow get a list of running apps, extract the one I want, get the HWND from that and then... hmm.... then what? :)

Jemine answered 27/12, 2010 at 13:58 Comment(0)
D
17

Use FindWindowEx() to find the handle (HWND) and then send the WM_SETTEXT message using SendMessage()

When using FindWindowEx you will need to first find the main window handle by using its class name. Then you will need to find the handle of whatever container the textbox is in, calling FindWindowEx, passing the handle of the parent (the window), and the class name of the container. You will need to repeat this until you reach the textbox. You can use a tool called Spy++ that is installed by default with Visual Studio to inspect the target application and find out the hierarchy of containers (all objects are really called windows in the API but I'm calling them containers in contrast with the top-level window) with their class names.

Dru answered 27/12, 2010 at 14:7 Comment(2)
Also, you MAY need to add a call to InvalidateRect as SendMessage would change it and I would see the value in Winspector but the value on my form stayed the same, so InvalidateRect will cause the repaintRebbecarebbecca
What if, the application contains more than one text box and I want to read the data from a specific text box. Is there any way to distinguish the handler of text boxes?Incommodious
K
3

Then SendMessage(), WM_SETTEXT

Kaela answered 27/12, 2010 at 14:1 Comment(0)
L
2

Instead of targeting a specific app you could just send keystrokes to the text field.

  private void button1_Click(object sender, EventArgs e)
    {
       System.Threading.Thread.Sleep(5000);
       SendKeys.Send(send_text);

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        send_text = textBox1.Text;            
    }
Lazos answered 21/6, 2014 at 19:20 Comment(3)
Kindly add some explanation as wellPolyamide
This worked awesome. Thanks so much for sharing! I can't believe it's down voted. Come on people, just because it's not the ideal solution in your context doesn't mean it's not useful!Sordino
This is the ideal way to go if you're trying to make a realistic, human-like auto typer. You could even type each letter individually in a for loop and add a random delay between each letter to make it more human-like. You could even make a system-wide hotkey to start typing by using the RegisterHotKey() function from user32.dll.Spider
A
0

you can use ClipBoard class to achieve the same

Asante answered 27/12, 2010 at 14:2 Comment(1)
but this will involve user handling instead of code executionCentro

© 2022 - 2024 — McMap. All rights reserved.