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.