简体   繁体   中英

Retrieving Java Swing Click events from another application

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?

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM