B4J Library jAWTRobot - invoke keyboard and mouse events, etc...

Roycefer

Well-Known Member
Licensed User
This library started out as a simple wrapper for Oracle's java.awt.Robot package but it evolved out of hand into a library of general utilities. Some of these functions appear to duplicate functionality already present in B4J however this library is designed to be used in both UI apps and non-UI apps. Consequently, B4J functionality that requires the use of a JFX object (fx.showExternalDocument(), for example) is duplicated here but without the use of any objects only found in UI apps.

Things you can do with this library:
  • programmatically move the mouse and invoke system MouseButtonPressed events or KeyPressed events and much more
  • access the system clipboard
  • get a variety of general operating system, hardware and JVM info
  • get a variety of JVM, process and system performance info (memory usage, CPU load, etc...)
  • take screen shots of arbitrary rectangles on the system's screens
  • run arbitrary commands on the command line
  • redirect Standard Error and Standard Out to file
  • open external files or URLs
  • improved Exception-related stuff
  • improved Thread-related stuff
  • restart your app
The library is very heavily commented so you need only read them to see what is possible.

EDIT (8JUL2015): Added hostNameAndIPAddress() method. Library version remains the same, but it is a new file.
EDIT (30JUL2015): Updated to 1.1. See post #12 for details.
EDIT (25OCT2015): Updated to 1.3. See post #20 for details.
EDIT (26OCT2015): Updated to 1.35. See post #27 for details.
EDIT (22NOV2015): Updated to 1.37. See post #30 for details.
EDIT (30NOV2015): Updated to 1.4. See post #31 for details.
EDIT (30JAN2016): Updated to 1.42. See post #34 for details.
EDIT (31JAN2016): Updated to 1.45. See post #35 for details.
EDIT (31JAN2017): Updated to 1.50. See post #79 for details.
EDIT (02FEB2017): Updated to 1.51. See post #85 for details.
EDIT (06FEB2017): Updated to 1.52. Minor bug fix.
EDIT (04APR2017): Updated to 1.54. Bug fix for relaunchSelfX methods.
EDIT (28DEC2017): Updated to 1.55. Added ScreenCapture2. Read comments for more info.
EDIT (09JAN2019): Updated to 1.59. RobotPaste debugging build
EDIT (14JAN2019): Updated to 1.61. Another RobotPaste2 debugging build
 

Attachments

Last edited:

jmon

Well-Known Member
Licensed User
Thanks a lot for this library. I was needing something that can gather operating system information, like totalSystemPhysicalMemory and numberOfProcessors.

Would it be possible to add:
- Computer name
- CPU Speed
- CPU Architecture
- IP Address / MAC Address ( i think I can get those with other libraries )
?

Thanks again
 

Roycefer

Well-Known Member
Licensed User
What do you mean by "computer name"? The name of the manufacturer? The model name? The Windows User Account Name? You can already get the latter by using B4J's
B4X:
GetSystemProperty("user.name","HAL-9000")
Here's a list of some more properties: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

CPU speed isn't available to the JVM through standard calls, however I've been using OpenCL to get that info. Unfortunately, my OpenCL libraries are huge and have tons of native dependencies; way overkill for that single call. And they only work on OpenCL-compliant systems. If you need some info about processor performance, you could always write a tiny little single-threaded benchmark and run it on a known system. Then when it ran on a new system, you'd know how that system's single-threaded performance compares to the known system. Multiply that by numberOfProcessors.....

There is a system property called "os.arch" that you can call with the aforementioned code, but it doesn't return the OS architecture but rather the bitness of the JVM. If you're running a 32-bit JVM on a 64-bit computer, it would return "32", for example. I suppose if it returned "64", you'd have a pretty strong clue that you weren't on a 32-bit computer, though.

I'm not sure about MAC address but I know you can get your local IP address with the jNetwork library. As for your external IP, you can probably just use http to hit up https://www.whatismyip.com/ .

I'm always on the lookout for new utilities to add to this library so if I come across ways to get those stats you requested, I'll update the library.
 

Roycefer

Well-Known Member
Licensed User
The library has been updated to 1.1. The previous library grew in a sort of ad-hoc manner and, because of that, was sort of sloppily put together. I've renamed almost all the functions so that they are grouped together by what they do. For example, before, functions for accessing JVM memory statistics were scattered all over the alphabet (and consequently all over the code completion menu). Now, they all start with "JVMMemory...", which groups them together. Also, the names are more descriptive. Some of the old ones were kind of cryptic.

A bunch more functionality was added to the clipboard section of the library. Now, almost all the fx.Clipboard functionality is duplicated without the use of JavaFX, which means you have almost all the functionality of fx.Clipboard when writing non-UI apps.

Java's System.nanoTime was exposed, giving you more precise benchmarking capabilities. Make sure to read the comments for SystemNanoTime so that you don't misuse it.
 

Beja

Expert
Licensed User
Thanks Royce,
Will test it soon.. I assume all the printable letters can now be printed on the window that has the focus.
We shall soon move the next project..
 

hibrid0

Active Member
Licensed User
Hi thanks for this library.
I want to paste an string to other Application. But the code work fine if I paste inside edittext if my app, but dont paste outside my app.
 

hibrid0

Active Member
Licensed User
Hi, I'm here againg...
The method RobotPaste not work on MAC.

I try to set the paste shortcut to Control+V or CMD+V and not work.
 
Last edited:

Roycefer

Well-Known Member
Licensed User
I updated the library to version 1.3 and added a few new methods. There's now RobotPaste2 and RobotPaste3 which use different methods for invoking the paste key-combo. See the comments on those methods for more details. One of those methods should work on Macs. Please report back on which, if any, of them work on Macs.

I've also added a JVMAddShutdownHook method which exposes Java's addShutdownHook() method. This method spawns a new (briefly lived) thread when the JVM is being shutdown under its own control (i.e. not being shut down by the OS). This allows you to execute some clean up tasks (releasing resources, saving settings, etc...) as the JVM is being shut down.
 
Top