B4J Question Read X and Y of mouse cursor

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

please, I have read the forum, but I cant find what I need.

I am finding solution, how can I read coordinates (x,Y) of mouse cursor on my screen

Then I am trying this:
B4X:
Sub Checkposition
 Dim eventdata As MouseEvent
 Dim joo As JavaObject = eventdata
  joo.InitializeStatic("MouseEvent")
 x = joo.RunMethod("getScreenX", Null)
 y = joo.RunMethod("getScreenY", Null) 
End Sub

I need checking the position continuously on my screen (monitor)
abd I get error:

java.lang.RuntimeException: Object should first be initialized (MouseEvent)

Please help me
Thank you very much
p4ppc
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
This will give the mouse position anywhere on the screen (not just in your application).
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim temp,robot As JavaObject
    Dim t As Timer
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("robot") 'Load the layout file.
    MainForm.Show
    initRobot
    t.Initialize("t",300)
    t.Enabled = True
End Sub
Sub t_tick
        Dim x,y As Int
        x = robot.RunMethod("getMouseX",Null) ' screen co-ordinate not the forms
        y = robot.RunMethod("getMouseY",Null) ' as x
        MainForm.Title = "Mouse X:" & x & " Y:" & y
End Sub
Sub initRobot
    robot = temp.InitializeStatic("com.sun.glass.ui.Application").RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)
End Sub
 
Upvote 0
Top