2

Does Java have the ability to get click events from an external Java application running on Windows 10? I have one external application that is running on my server that is built with Swing. Unfortunately, I do not have access to it's source code. I was wondering if there is any way to get notified when a user clicks on a text box in that external application.

Is this possible with something like Java Access Bridge?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
azdev125
  • 23
  • 3
  • What type of application and what type of device is it on? – Raheel138 May 15 '17 at 20:46
  • The external application is a Java application with a GUI built with Swing. The application is running on a Windows 10 PC. My application is written in Java and will also run on Windows. – azdev125 May 15 '17 at 20:53
  • I believe the only reliable way of doing this is having access to the source code of the java application that you are reading the input from then having some form of transmitting actions to the other application like a text file that constantly updates. As long as you don't have access to the code for the other application, it will not be possible for you to get actions that occur in a non-limited way. – Raheel138 May 15 '17 at 22:49
  • *"get click events from an external Java application"* Why? – Andrew Thompson May 16 '17 at 01:16
  • We want to be able to tell when the user is entering in information so we can perform some tasks on our end. – azdev125 May 16 '17 at 14:11
  • By the way... Both these applications will reside on the same computer. – azdev125 May 16 '17 at 14:22
  • Do you have a hand on the launcher script? (or is it an executable jar?). Would you be able to add something of yours to the bootstrap classpath? – Hugues M. May 16 '17 at 17:48
  • It is an executable jar. I may be able to add to the bootstrap classpath. – azdev125 May 16 '17 at 17:56
  • Related: [Automated tests for Java Swing GUIs](https://stackoverflow.com/q/91179/3357935) – Stevoisiak Oct 19 '17 at 13:11

1 Answers1

0

You could intercept events of your choosing and post data to a server (or messages to process, or log to file... your call).

To intercept events, I see those 2 directions:

1) You could point the executable jar to your own main class, which would install a custom AWT event queue (this part is explained here), and then run the actual main class, passing arguments unmodified. (We've done that at work a few years ago to implement some kind of screen-grabbing tool)

1b) Easier than a custom AWT event queue, if you just want to record events, AWT has Toolkit.addAWTEventListener for you:

Toolkit.getDefaultToolkit().addAWTEventListener(
    this::handleEvent,
    AWTEvent.KEY_EVENT_MASK | AWTEvent.ACTION_EVENT_MASK // The kind of events you want
);
/*...*/
private void handleEvent(AWTEvent awtEvent) {
    if (eventIsInteresting(awtEvent)) { // Do your own filtering
        logEvent(awtEvent); // Collect, send message, whatever
    }
}

(this is used in GUI testing tools for example, to record test scenarios)

2) Alternatively, you could modify source of select JRE classes (e.g. ActionEvent.java), insert your code at strategic points, compile that, assemble those classes to a jar file, and prepend that to the bootclasspath with -Xbootclasspath/p:./hack.jar. (We've done that at work a few years ago to "backport" a bugfix from Java 8 to Java 7)

Implementation details are left as an exercise for the reader.

Community
  • 1
  • 1
Hugues M.
  • 19,846
  • 6
  • 37
  • 65