B4J Question Multitouch

Hi,

I am working on multitouch in B4J and I have some questions.
Idea is to have pane same size as form, and when there is touch event on pane i get data on x, y, id of touch...

My code is here:
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TouchPane As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Multitouch") 'Load the layout file.
    MainForm.Show
    Dim jo As JavaObject = TouchPane
    Dim e As Object = jo.CreateEvent("javafx.event.EventHandler", "Touch", False)
    jo.RunMethod("setOnTouchMoved", Array(e))
End Sub

Sub Touch_Event (Method As String, Arg() As Object)
    If Arg <> Null Then
        Log(Arg(0))
    End If
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
Here is Log:
B4X:
TouchEvent [source = PaneWrapper$ConcretePaneWrapper$NonResizePane@7f641f8c, target = PaneWrapper$ConcretePaneWrapper$NonResizePane@7f641f8c, eventType = TOUCH_MOVED, consumed = false, touchCount = 1, eventSetId = 117, touchPoint = TouchPoint [state = MOVED, id = 1, target = PaneWrapper$ConcretePaneWrapper$NonResizePane@7f641f8c, x = 341.0, y = 390.0, z = 0.0, pickResult = PickResult [node = PaneWrapper$ConcretePaneWrapper$NonResizePane@7f641f8c, point = Point3D [x = 341.0, y = 390.0, z = 0.0], distance = 1119.6152422706632]]
TouchEvent [source = PaneWrapper$ConcretePaneWrapper$NonResizePane@7f641f8c, target = PaneWrapper$ConcretePaneWrapper$NonResizePane@7f641f8c, eventType = TOUCH_MOVED, consumed = false, touchCount = 2, eventSetId = 133, touchPoint = TouchPoint [state = MOVED, id = 2, target = PaneWrapper$ConcretePaneWrapper$NonResizePane@7f641f8c, x = 218.0, y = 433.0, z = 0.0, pickResult = PickResult [node = PaneWrapper$ConcretePaneWrapper$NonResizePane@7f641f8c, point = Point3D [x = 218.0, y = 433.0, z = 0.0], distance = 1119.6152422706632]]
With Log(Arg(0)) I can get lot of data regarding touch, like touchCount, id, x, y.
Problem is that it's all in same string. I can parse string and get data I need, but is there some other way to do this?
Something where I can extract exact parameter.
Like: jo.RunMethod("setOnTouchMoved.touchCount", Array(e))?

Thanks
 

Erel

Administrator
Staff member
Licensed User
Thanks Erel, that is what I needed.

Here is full code if anyone needs it:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TouchPane As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Multitouch") 'Load the layout file.
    MainForm.Show
    Dim jo As JavaObject = TouchPane
    Dim e As Object = jo.CreateEvent("javafx.event.EventHandler", "Touch", False)
    jo.RunMethod("setOnTouchMoved", Array(e))
End Sub

Sub Touch_Event (Method As String, Arg() As Object)
    If Arg <> Null Then
        Dim TouchEvent As JavaObject = Arg(0)
        Log("Touch count: " & TouchEvent.RunMethod("getTouchCount", Null))
        Dim TouchPoints As List = TouchEvent.RunMethod("getTouchPoints", Null)
        For Each point As JavaObject In TouchPoints
            LogError($"X: ${point.RunMethod("getX", Null)} Y: ${point.RunMethod("getY", Null)}"$)
        Next
    End If
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
To make it work you also need to create pane in Visual designer, rename it to TouchPane (Private TouchPane As B4XView).


Thanks again Erel
 

bjfhs

Active Member
Licensed User
Thanks Erel, that is what I needed.

Here is full code if anyone needs it:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TouchPane As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Multitouch") 'Load the layout file.
    MainForm.Show
    Dim jo As JavaObject = TouchPane
    Dim e As Object = jo.CreateEvent("javafx.event.EventHandler", "Touch", False)
    jo.RunMethod("setOnTouchMoved", Array(e))
End Sub

Sub Touch_Event (Method As String, Arg() As Object)
    If Arg <> Null Then
        Dim TouchEvent As JavaObject = Arg(0)
        Log("Touch count: " & TouchEvent.RunMethod("getTouchCount", Null))
        Dim TouchPoints As List = TouchEvent.RunMethod("getTouchPoints", Null)
        For Each point As JavaObject In TouchPoints
            LogError($"X: ${point.RunMethod("getX", Null)} Y: ${point.RunMethod("getY", Null)}"$)
        Next
    End If
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
To make it work you also need to create pane in Visual designer, rename it to TouchPane (Private TouchPane As B4XView).


Thanks again Erel
I copy your code to my project,but when I move the mouse I can't get anything,the touch event don't raised.
 

Attachments

bjfhs

Active Member
Licensed User
I find that it can't work with the touch panel of notebook,but can work on a pc with touch screen.
 
I copy your code to my project,but when I move the mouse I can't get anything,the touch event don't raised.
It is set to work only with touch.
If you want it to work with both mouse and multitouch you need to add mouse listener:
B4X:
Dim mt As Object = jo.CreateEvent("javafx.event.EventHandler", "MClick", False)
jo.RunMethod("setOnMousePressed", Array(mt))
Dim mr As Object = jo.CreateEvent("javafx.event.EventHandler", "MRelese", False)
jo.RunMethod("setOnMouseReleased", Array(mr))
Dim md As Object = jo.CreateEvent("javafx.event.EventHandler", "MoveMouse", False)
jo.RunMethod("setOnMouseDragged", Array(md))
 
Last edited:
Top