B4J Question Multitouch

Discussion in 'B4J Questions' started by Ivan Vidak, Jul 4, 2019.

  1. Ivan Vidak

    Ivan Vidak Member

    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:
    Code:
    #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 StringAs Boolean
        
    Return True
    End Sub
    Here is Log:
    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Ivan Vidak likes this.
  3. Ivan Vidak

    Ivan Vidak Member

    Thanks Erel, that is what I needed.

    Here is full code if anyone needs it:
    Code:
    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 StringAs 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
     
    Erel and rboeck like this.
  4. bjfhs

    bjfhs Active Member Licensed User

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

    Attached Files:

  5. bjfhs

    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.
     
  6. Ivan Vidak

    Ivan Vidak Member

    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:
    Code:
    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: Jul 15, 2019
  7. Ivan Vidak

    Ivan Vidak Member

    I have touch screen monitor and it work fine on pc, don't have touch notebook so i can't try it.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice