B4J Question jAWTRobot RobotMouseButtonPress

ThRuST

Well-Known Member
Licensed User
Longtime User
How can I use the RobotMouseButtonPress with ReleaseMouseButton() in jAWTRobot library version 1.45 to detect a mouseclick outside a form?
A code example would be great, thanks.
 
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
The jAWTRobot library doesn't detect mouse clicks, though it can tell you the mouse position (relative to global screen coordinates). To detect mouse clicks, you should first try to accomplish your goals using the JavaFX event system standard in B4J UI apps. If that doesn't work, you can use the jNativeHookB4J library to detect mouse clicks.

The jAWTRobot library allows you to invoke MousePressed events and MouseReleased events from within your program (and they can occur outside your UI window).

Example using the jNativeHookB4J library:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim nh As NativeHook
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    nh.Initialize("nh", Me).startNativeMouseInputListener   '<-----DON'T FORGET TO UNREGISTER NH WHEN DONE!!!!
   
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
End Sub

'This will be raised whenever there's a MouseClicked event.
'This doesn't run in the Main Thread. 
'Use the Threading library's thread.RunOnGuiThread() if you need to access the UI from this sub.
Sub nh_NativeMouseClicked(nme As NativeMouseEvent) As Boolean
    Log($"nh_NativeMouseClicked(X:${nme.X},Y:${nme.Y},ClickCount:${nme.ClickCount},Button${nme.MouseButton})"$)
    Return False     'Not consuming the event.
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Nice. How to detect which button that was clicked and where? should nme.MouseButton = 1 then be used inside nh_NativeMouseClicked or as a freestanding sub?
Also how to unregister the StartNativeMouseInputListener, must this allways be done when exiting the program? Thanks for pointing this out.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Don't try to set any nme values. The nme Object is given to you as a parameter in the nh_NativeMouseClicked event sub. It contains all the info about the mouse input event (like x,y values, mouse button and click count). You'll have to run the code to see which mouse button is represented by which integer, it's been a while since I tested it. But it should be the same across platforms.

As for unregistering, yes, you definitely want to unregister the NativeHook before your program closes. If you have too many registered NativeHooks, you'll have difficulty registering new ones and other programs that rely on system input events will suffer interference. If this happens, just reboot your computer and it will clear the dangling NativeHooks. However if you properly unregister your NativeHooks when you're done with them, you won't have this problem. The jAWTRobot library has a JVMShutdownHook function that allows you to run some code as the JVM is shutting down under its own control so you can perform clean up tasks. This is a good place to unregister NativeHooks in non-UI apps. In UI apps, you can use the MainForm_Closed event or something.

B4X:
nh.endNativeMouseInputListener   'This stops the NativeMouseInputListener. This is NOT the same as unregistering the NativeHook
nh.unregisterNativeHook   'unregisters the NativeHook

All these functions should appear in the IDE's code completion menu along with copious comments explaining their usage.
 
Upvote 0
Top