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

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

  • jAWTRobot.zip
    11.1 KB · Views: 699
  • jAWTRobot1.1.zip
    13.6 KB · Views: 444
  • jAWTRobot1.3.zip
    15.1 KB · Views: 389
  • jAWTRobot1.35.zip
    15.5 KB · Views: 416
  • jAWTRobot1.37.zip
    15.7 KB · Views: 399
  • jAWTRobot1.4.zip
    16.1 KB · Views: 437
  • jAWTRobot1.42.zip
    16.6 KB · Views: 355
  • jAWTRobot1.45.zip
    19.7 KB · Views: 577
  • jAWTRobot1.50.zip
    21.6 KB · Views: 399
  • jAWTRobot1.51.zip
    22.1 KB · Views: 358
  • jAWTRobot1.52.zip
    22.1 KB · Views: 434
  • jAWTRobot1.54.zip
    22.1 KB · Views: 567
  • jAWTRobot1.55.zip
    22.8 KB · Views: 635
  • jAWTRobot1.59.zip
    22.6 KB · Views: 282
  • jAWTRobot1.61.zip
    23.2 KB · Views: 965
Last edited:

ThRuST

Well-Known Member
Licensed User
Longtime User
how fast is your computer? do you mind testing the floatingcontextmenu and try the fast click. if so will the menu refresh fast enough to not be stuck drawn at the screen. I think this has something to do with backbuffering. Nothing should be drawn to the screen before it's properly prepared in the backbuffer. That might be a weakness with my videocard as the computer is really slow, but I'm not sure. A benchmarking for specific routines is a really nice implementation. Your library sure is one of the very best available. Please design a CSS theme for the contextmenus, it's great fun and buy the logitech mouse, with your library great things is possible :)
 

stevel05

Expert
Licensed User
Longtime User
@ThRuST Check the original Floating Menu thread, this issue should now be fixed.
 

rboeck

Well-Known Member
Licensed User
Longtime User
Today i was able to install javafx on a virtual ubuntu session and all my code, primary the robotpaste function worked without any modifications! Thanks for your work!
 

ThRuST

Well-Known Member
Licensed User
Longtime User
That's great I will try that again too and test it on both Windows and MacBook. Btw how can I implement keyboard shortcuts into my application? for example a three key combination like SHIFT+ALT+1 what will the code look like give an example please.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Hey Roycefer I assume you're the author to the NativeHook and AWTRobot libraries, so I wonder what's your real first and lastname? I want to add your name among the credits in my B4J project. If I can lift you up and promote you with a higher status it's my honor to do so. If you're willing to risk your reputation by an unknown stranger like me!!? but who knows, maybe one day my name will be up in the spotlight and that'll chew some lamers who did not believe it will ever happen. The ball is all yours pal :D
 

Roycefer

Well-Known Member
Licensed User
Longtime User
That's very kind but you don't need to credit me by name. You may want to check out the Java jNativeHook library's Github page https://github.com/kwhat/jnativehook (this is the Java library that the jNativeHookB4J library wraps). Maybe that guy would like some credit.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I see, but you did a great job in converting this wrapper to B4J that's very special since B4X is the best development tool ever created :)
How about the jAWTRobot library who wrote that for java? Good programming ethics is to credit everyone who is any way contributed to the final application so I guess a persons nickname is enough to be recognized. That's how it works in the demoscene so people get famous from their alter ego. I've been around long enough to find out myself :cool:
 

ThRuST

Well-Known Member
Licensed User
Longtime User
btw your nickname is your real name? Roy Cefer? or Roy Roycefer Cefer, or how about Roy "The Robot" Cefer :D
 

Roycefer

Well-Known Member
Licensed User
Longtime User
The jAWTRobot library mostly just wraps standard Java APIs. I guess you could credit the now defunct Sun Microsystems.
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi Roycefer,

last week i bought an old iMac with El capitan and tried my routines, which are using robotpaste - and there was no pasting. So i checked: the clipboard has the string inside, i can manually paste with cmd + v and i tried rb2.RobotSpecialKeyCombo("meta_v") and it worked without problems and there is no need to change anything. I only wanted to report the situation.
In the last week i made two linux java test installations (ubuntu and mint), so i can now test for you for all three variants.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Hi Rboeck, as you can test on various platforms do you mind evaluating this sub for me since I use it for my b4j project I'm working on.
Please update this sub and post it here so I can update my project so it will work on the platforms you mentioned. Thanks

B4X:
Dim os As String = GetSystemProperty("os.name", "").ToLowerCase

If os.Contains("win") Then

        ' Windows
           fx.Clipboard.SetString ("hello")

        AWTRobot.RobotSpecialKeyCombo("ctrl_v")
  
  
    Else If os.Contains("mac") Then
  
        ' Macintosh
        fx.Clipboard.SetString ("world")

        AWTRobot.RobotSpecialKeyCombo("meta_v")

    Else
  
           ' Linux
  
    End If
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Btw I posted this earlier in this thread but everybody believed robotpaste would work, but from my testings on my Macbook I had to come up with the sub above. Cheers
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Attached is the jAWTRobot library version 1.49. It includes a method called RobotPasteBeta that uses a new means of cross-platform pasting. Let me know if it works on all your various operating systems. If not, let me know what the behavior is and what the Exceptions are, if any. If it does work, I'll rename it RobotPaste for version 1.50 and remove the old RobotPaste.

There are also some new Exception-related methods (unrelated to this Paste issue). Read their comments to learn more.
 

Attachments

  • jAWTRobot1.49.zip
    21.7 KB · Views: 240

ThRuST

Well-Known Member
Licensed User
Longtime User
Thanks Roycefer, RobotPasteBeta works well on my PC and MacBook. Just tested and it was sucessful :eek::)
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Great. I'll wait for confirmation of same from rboeck. If it all turns out ok, I'll post version 1.50 to the original post in this thread.
 

rboeck

Well-Known Member
Licensed User
Longtime User
Thanks, your work was sucessfull, RobotPasteBeta worked on linux mint, w7 and now on mac el capitan! I found your new methods - can you give us a hint, for which usecases they are made for?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Version 1.50 has been added to the original post for this thread. It has the new RobotPaste method which should work on all supported platforms.

It also has a bunch of new methods for Exception-related stuff. These are mostly experimental, though if you run into problems with them, post them here. Some examples:
  • robo.B4JCallingThreadStackTraceGet() allows you to get the Stack Trace of the calling Thread. This can be useful during troubleshooting. You can also use it in a method to see what method called the current method (and what method called that one, etc....).
  • robo.B4JGetStackTrace() gives you the stack trace of a B4X Exception that you pass it as a parameter (probably one that you caught in a Try-Catch block).
  • robo.B4JCallingThreadStartProtectedMessageLoop() starts a "protected" MessageLoop for the calling Thread. This will prevent the Thread from crashing in the event of an Exception. Instead, you'll be able to handle the Exception in an Event method and decide if you want to continue the MessageLoop. I have found a lot of use for this in server applications.
Read the comments on those methods for more info.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Roycefer, great work. I would like you to add a way to check if the internet connection is present, for example IsRobotConnection true or false that would be really useful.
 
Top