Insert text into the textbox of another application
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.
Programatically paste text into text box in another application
You can bring up the other app and send a key combination... something like:
[DllImport ("User32.dll")]
static extern int SetForegroundWindow(IntPtr hwnd);
public static void PasteToApplication(string appName)
{
var proc = Process.GetProcessesByName(appName).FirstOrDefault();
if(proc != null)
{
var handle = proc.MainWindowHandle;
SetForegroundWindow(handle);
SendKeys.SendWait("^v");
}
}
This should bring up the other app's window and send a ctrl-v command. With a bit of experimentation, you could find out the exact handle of the control you want to send the paste to, and set focus on it aswell
UI Automation - Set Text for a another application's TextBox
You should first check for the availability of the ValuePattern
pattern:
- If the
ValuePattern
pattern is available, use itsSetValue
method. - Else use one of the following solutions:
- Set the focus to the control and use
SendKeys
to clear and set the text. - Or Use
SendMessage
and sendWM_SETTEXT
message to set the text,
- Set the focus to the control and use
Example
var notepad = System.Diagnostics.Process.GetProcessesByName("notepad").FirstOrDefault();
if (notepad != null)
{
var root = AutomationElement.FromHandle(notepad.MainWindowHandle);
var element = root.FindAll(TreeScope.Subtree, Condition.TrueCondition)
.Cast<AutomationElement>()
.Where(x => x.Current.ClassName == "Edit" &&
x.Current.AutomationId == "15").FirstOrDefault();
if (element != null)
{
if (element.TryGetCurrentPattern(ValuePattern.Pattern, out object pattern))
{
((ValuePattern)pattern).SetValue("Something!");
}
else
{
element.SetFocus();
SendKeys.SendWait("^{HOME}"); // Move to start of control
SendKeys.SendWait("^+{END}"); // Select everything
SendKeys.SendWait("{DEL}"); // Delete selection
SendKeys.SendWait("Something!");
// OR
// SendMessage(element.Current.NativeWindowHandle, WM_SETTEXT, 0, "Something!");
}
}
}
In case of using SendMessage
make sure you add the following declarations to the class:
[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern int SendMessage(int hWnd, int uMsg, int wParam, string lParam);
const int WM_SETTEXT = 0x000C;
You can read about the approach:
- Add Content to a Text Box Using UI Automation
How to insert text to another program's textbox using java
As Jonathon noted in the comment, you can try using java.awt.Robot
. But you'd need to know the exact location of target text field, and have it visible on the screen.
You can have something like:
Robot robot = new Robot();
robot.mouseMove(xCoord, yCoord);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(50);
robot.keyPress(51);
Apart from that, you'd need the application to provide some native API to interact with its form, and use it via JNI.
Writing/Reading text in the Text box of a application from another application
Use the GetWindowText
method to read the text and SetWindowText
to set the text.
However, if you control both applications, you really should think about implementing some kind of inter process communication either using old school named pipes, shared memory or up-to-date WCF.
Related Topics
Finding the Concrete Type Behind an Interface Instance
Xamarin Android Alarm Manager Issue
Getting a Request.Headers Value
Mailmessage C# - How to Make It HTML and Add Images etc
Setup Database (Sqlite) for Unity
Execute SQL Script on SQL Server Using C#
Find If Lista Contains Any Elements Not in Listb
Sort Datagridview Columns in C#? (Windows Form)
Logoff Interactive Users in Windows from a Service
Load Different CSS File Based on Browser
Can You Call a C# Dll from a C Dll
Com Object That Has Been Separated from Its Underlying Rcw Cannot Be Used
Show Data in ASP.NET HTML Table