Question

Is there a way of retrieving event info and UI element data from an external Java desktop application? Specifically, I need to log when the value of a specific textbox changes and retrieve data from several other input elements.

This is for a proof-of-concept project for a customer (not malware;-)).

The Java application already exists and cannot be added to or re-compiled. The "listening" application can be built using .NET or Java. I've found some limited info on Windows event hooks and the Java Accessibility API but have hit a block, mainly due to my lack of knowledge.

I've no idea if this is easily done - thoughts appreciated.

Was it helpful?

Solution

One solution that comes to my mind is to write a Java program that wraps the existing one.

Let's say the original program is distributed in a single .jar. Then, you would write a Java class, compiled with this jar as a dependency, run with this jar in the classpath, and containing a main function that calls the main function of the jar.

public class WrapMyApp {
    public static void main(String[] args) {
        com.myorg.MyApp.main(args);
    }
}

So, using this technique, you are in control of the JVM, and can inject code before or after the call to main.

What trick can you use to retrieve some infos from the GUI ? Well, if you are lucky, maybe you could try to access the GUI component through the reflexion API. Another solution is to traverse the GUI, starting from the JFrame. This implies that the GUI layout is known in advance, and that it's static enough. Example :

    JWindow w = (JWindow) Window.getWindows()[0];
    JPanel p = (JPanel) w.getContentPane().getComponent(2);
    JTextArea t = (JTextArea) p.getComponent(5);
    String s = t.getText();

You can use tools like SwingExplorer to explore the GUI layout, in order to discover where your components are located.

If this doesn't work, you can try to hook to the EventQueue, listening to the key events :

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

        public void eventDispatched(AWTEvent event) {
            // useful code here
        }
    }, AWTEvent.KEY_EVENT_MASK);

Don't forget to wrap your code inside a SwingUtilities.invokeLater(), when trying to access GUI components.

OTHER TIPS

See this link:

External Key listener

You can listen to precionamentos key, within this same API you can capture the active window title in monento to make sure that the application is expected, and thus trigger internal events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top