0

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?

Community
  • 1
  • 1
Volatile
  • 677
  • 2
  • 8
  • 17
  • 2
    Probably you will have to plug-in (with JNI) into the messaging system that informs application of event (like "mouse click" or "R key pressed"). I suggest you explore solution like `AutoHotKey` to see if they suit you. – SJuan76 Dec 21 '12 at 23:21
  • 1
    I also agree with @SJuan76. Consider also looking at AutoIt Version 3 which is similar to AutoHotKey and likewise a powerful Windows scripting tool. – Hovercraft Full Of Eels Dec 21 '12 at 23:30
  • I appreciate the ideas about AutoHotkey, and I already know it exists. But I would really like to see if it was possible to solve this problem solely through Java. :) – Volatile Dec 21 '12 at 23:32

1 Answers1

3

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373