I am trying to make a little Java application that takes input from me, opens another application (Windows Calculator / “calc”) and feeds that application with my input.
At the moment, I am trying to do this to the simple Windows Calculator, but it doesn’t seem to work with the conventional means:
public Feeder(String processID) throws Exception {
rt = Runtime.getRuntime();
proc = rt.exec("calc");
input = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
}
public void sendCommand(int cmd) throws Exception {
input.write(cmd);
input.flush();
input.close();
proc.waitFor();
}
(Partially taken from another source, credit due to author)
However, contrary to the above code, which sends to another command-line process, WinCalc is graphical. Is it still possible to send input to it without having to go through all sorts of trouble like reverse engineering?
You can use java.awt.Robot to send text to the current active window and to move and click the mouse for you. If you need more advanced message passing, then you’ll need JNI or (my recommendation) JNA.
Other options include tying your application to AutoHotKey or AutoIt V3.
Myself, I’ve had success running other applications with a combination of JNA, Robot, and AutoIt, using the one that works best for that situation.