Sub Globals
Dim pnl As Panel
End Sub
Sub Activity_Create(FirstTime As Boolean)
pnl.Initialize("")
Dim jo As JavaObject = pnl
Dim e As Object = jo.CreateEvent("android.view.View.OnGenericMotionListener", _
"pnl", False)
jo.RunMethod("setOnGenericMotionListener", Array(e))
Activity.AddView(pnl, 0, 0, 100%x, 100%y)
End Sub
Sub pnl_Event(methodName As String, args() As Object) As Boolean
Dim motionEvent As JavaObject = args(1)
Dim action As Int = motionEvent.RunMethod("getAction", Array())
Dim deviceId As Int = motionEvent.RunMethod("getDeviceId", Array())
Log("action: "&action)
Log("deviceId: "&deviceId)
Return True
End Sub
I don't have any joysticks to test with, but I had a look at this, which looks like it could be implemented with JavaObject in b4a.
B4X:Sub Globals Dim pnl As Panel End Sub Sub Activity_Create(FirstTime As Boolean) pnl.Initialize("") Dim jo As JavaObject = pnl Dim e As Object = jo.CreateEvent("android.view.View.OnGenericMotionListener", _ "pnl_OnGenericMotionListener", False) jo.RunMethod("setOnGenericMotionListener", Array(e)) Activity.AddView(pnl, 0, 0, 100%x, 100%y) End Sub Sub pnl_OnGenericMotionListener(v As View, motionEvent As Object) As Boolean Log("GenericMotionEvent received") Return True End Sub
You are missing the JavaObject library. You'll notice when you try to compile the code with this library in place that pnl was declared in the correct place too.
jo.RunMethod("setOnGenericMotionListener", Array as Object(e))
Sorry, that's my fault - the error is because b4a v3.8 has allowed 'Array' to be used as shorthand for 'Array as Object'. So it should be....
.... for users of versions <3.8.B4X:jo.RunMethod("setOnGenericMotionListener", Array as Object(e))
#AdditionalJar: ActivityEx
#Extends: anywheresoftware.b4a.objects.ActivityEx
Sub Activity_OnGenericMotionEvent(MotionEvent As Object)
Log(MotionEvent)
End Sub
Download the attached jar file and copy it to the additional libraries folder.
Add these lines to the activity:
B4X:#AdditionalJar: ActivityEx #Extends: anywheresoftware.b4a.objects.ActivityEx
This will allow you to handle the activity onGenericMotionEvent:
B4X:Sub Activity_OnGenericMotionEvent(MotionEvent As Object) Log(MotionEvent) End Sub
You can now use JavaObject to access MotionEvent.
Dim jme As JavaObject = MotionEvent
Log(jme.RunMethod("getX", null))
Log(jme.RunMethod("getY", null))