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: 1,105
  • jAWTRobot1.1.zip
    13.6 KB · Views: 734
  • jAWTRobot1.3.zip
    15.1 KB · Views: 693
  • jAWTRobot1.35.zip
    15.5 KB · Views: 725
  • jAWTRobot1.37.zip
    15.7 KB · Views: 698
  • jAWTRobot1.4.zip
    16.1 KB · Views: 753
  • jAWTRobot1.42.zip
    16.6 KB · Views: 667
  • jAWTRobot1.45.zip
    19.7 KB · Views: 886
  • jAWTRobot1.50.zip
    21.6 KB · Views: 701
  • jAWTRobot1.51.zip
    22.1 KB · Views: 639
  • jAWTRobot1.52.zip
    22.1 KB · Views: 751
  • jAWTRobot1.54.zip
    22.1 KB · Views: 876
  • jAWTRobot1.55.zip
    22.8 KB · Views: 927
  • jAWTRobot1.59.zip
    22.6 KB · Views: 584
  • jAWTRobot1.61.zip
    23.2 KB · Views: 1,631
Last edited:

Gandalf

Member
Licensed User
Longtime User
Hi,

I'm trying to do a screenshot with the code
B4X:
    rur.ScreenCurrentRectangleSetAsArbitrary(0, 0, rur.ScreenGetDimensions(0), rur.ScreenGetDimensions(1))
    rur.ScreenCaptureToFile("screenshot.png")
and the result has resolution of my screen set correctly to 1920x1080, but screenshot itself takes only part of the image (see below). This PC is laptop with only one screen. What I'm doing wrong?
screenshot.png

UPDATE:
This is connected to Windows 10 scaling feature:
1621492735402.png

If I set it to 100%, screenshot takes all resulting image. But my vision is not so good to work with such scaling. Other third-party desktop capturing tools can work with any scaling somehow, producing screenshots with real resolution. Can it be done with this library also?
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
Wouldn't be easier to take a 100% screenshot, which works, and then resume the resulting image to 125%?
 

Gandalf

Member
Licensed User
Longtime User
Wouldn't be easier to take a 100% screenshot, which works, and then resume the resulting image to 125%?

I'm just curious if it is possible to get better resolution without scaling change. By the way, old screen capturing method which uses javaObject also suffers from scaling, but resulting image just has reduced resolution without black areas at right and bottom.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Have you tried using smaller or larger rectangles? If this app is only going to be for your own use, you can hard-code in your known scaling factor.
 

Gandalf

Member
Licensed User
Longtime User
Have you tried using smaller or larger rectangles? If this app is only going to be for your own use, you can hard-code in your known scaling factor.
Yes, I tried. The effect persists - at 125% scaling screenshot resolution is 1536x864 while screen is set to 1920x1080. Unfortunately this app is not for my use only, it is intended to be used in relatively critical process of customer's company.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
How are your inline Java skills? Try this out:
B4X:
    /**
     * Assume 96 dpi is 100% screen scaling.
     * @return the system's screen resolution in dots/inch
     */
    public int ScreenGetResolutionInDpi()
    {
        return java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
    }
Try this as a means of getting the scaling factor and then adjusting your rectangles accordingly. Let me know if it works. If so, I'll add it to the library.
 

Gandalf

Member
Licensed User
Longtime User
My inline Java skills are quite poor but enough to run this:
B4X:
Dim toolkit As JavaObject
toolkit.InitializeStatic("java.awt.Toolkit")
Dim res As Int = toolkit.RunMethodJO("getDefaultToolkit", Null).RunMethod("getScreenResolution", Null)
Log(res)
It returns 96 at 100% scaling and 120 at 125% scaling. But adjusting rectangle size to full resolution gives the same black area with smaller picture at top-left as in my first post here. The only thing I could do is to get rid of the black area using this method - and get the result like without using library.
 
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
Based on my preliminary search, it doesn't look like there's a standard API for battery level exposed in Java which means it won't be available with this library.

I've seen some solutions that use native access.
 
Top