B4J Question ROBOT MOUSEPRESS NOT WORK Is there a way to use the function of pressing the mouse keys automatically?

JHDANDROID

New Member
Hello,

Is there a way to use the function of pressing the mouse keys automatically? in the style of the ROBOT class.

I am using part of the code:
Code Version 7 java:
    Dim robot As JavaObject
    robot.InitializeNewInstance("javafx.scene.robot.Robot", Null)
    '.....
  
    robot.RunMethod("mousePress", Array(1))
    Sleep(300)        ' delay 5 seconds'

but it only worked for version 7 of java.



I currently have version 11 of Java and it gives me this error:
java.lang.RuntimeException: Method: mousePress not matched.

Note: I have reviewed the threads referring to the use of that function in which EREL recommends using but they are from the year 2014 using java v7.

And I also saw a recent post (java Version 11) that demonstrates how to automatically press keys using ROBOT Class and KEYCODE, but it only works for keys.


Very Thanks Much.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Full example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Dim robot As JavaObject
    Private Button2 As B4XView
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    robot.InitializeNewInstance("javafx.scene.robot.Robot", Null)
End Sub


Private Sub Button1_Click
    Dim x = Button2.Left + 20dip, y = Button2.Top + 20dip As Double
    Dim form As Form = B4XPages.GetNativeParent(Me)
    robot.RunMethod("mouseMove", Array(form.WindowLeft + x, form.WindowTop + y + 30dip)) 'rough compensation for the title
    Dim buttons As JavaObject
    Dim btn As JavaObject
    btn.InitializeStatic("javafx.scene.input.MouseButton")
    buttons.InitializeArray("javafx.scene.input.MouseButton", Array(btn.GetField("PRIMARY")))
    robot.RunMethod("mouseClick", Array(buttons))
End Sub

Private Sub Button2_Click
    Log("test")
End Sub
 
Upvote 0
Top