Detect how hard the screen is tapped?

rgately

Member
Licensed User
Longtime User
Is there a way to detect how hard (velocity) the screen is tapped? I would like to make some buttons respond to how hard they are tapped.

I had hoped to accomplish this with the PhoneAccelerometer. But after toying around with it, I'm not sure its feasible. I'd like to get the velocity information when a button is pressed down. Any suggestions?

For reference: the iPad2/GarageBand keyboard responds well to velocity, how hard to keys were tapped. I believe GarageBand gets the velocity information from the accelerometer.

Thanks
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can do it with the reflection library:
B4X:
Sub Globals
   Dim btn As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)

   Dim r As Reflector
   Dim btn As Button
   btn.Initialize("")
   Activity.AddView(btn, 10dip, 10dip, 300dip, 300dip)
   r.Target = btn
   r.SetOnTouchListener("btn_touch")
End Sub

Sub btn_touch (viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
   Dim r As Reflector
   r.Target = motionevent
   Dim pressure As Float
   pressure = r.RunMethod("getPressure")
   Log(pressure)
   Return True
End Sub
Pressure will be a value between 0 to 1 (can also be more than 1 according to the docs).
 
Upvote 0

rgately

Member
Licensed User
Longtime User
Erel,

Thanks for the sample code. I tried it but it only reports one value (0.1568627506494522) irregardless of how hard I tap the button. Is there more to it?

I noticed that the Gesture library also has a SetOnTouchListener.

I will see what I can figure out.

PS: What a great program B4A is.
 
Upvote 0

MikieK

Member
Licensed User
Longtime User
I still get the same error. I thought it was because btn_touch would be a B4a event for btn, so I renamed it to "btntouched" (in both places), with no luck.
 
Upvote 0

rgately

Member
Licensed User
Longtime User
Thanks VB1992 for your code. I hadn't thought about using getSize. It is interesting and if I can't get pressure then at least I can get size.

My Samsung Tab 7 WIFI is only 2.2.1 so maybe thats why it doesn't respond to GetPressure.
 
Upvote 0
Top