auto repeat

mbisso

Member
Licensed User
Longtime User
Is there a way to have a button press auto repeat if the user keeps the button pressed?

I have tried with a timer inside the button_click (or down) sub, but it seems that the sub does not exit until the button is released. And it does not re-enter even if the button remains pressed.

Sub rf_inc_click

If old_repeat <> repeat Then
If selected_rf <= 95 Then
selected_rf = selected_rf + 5
rf_power_value.text = selected_rf & "%"
Play(11)
draw_rf_power
End If
old_repeat = repeat
End If

End Sub

Sub inc_dec_repeat_Tick
repeat = repeat + 1 ' increment repeat timer every 500 ms
End Sub

That does not work because the sub never returns until the button is released. It does not loop on itself, using log I was able to determine that it only goes through the loop 1 time. I placed a log("done") just before the button_click (or down) end sub and found that it returns to the sender but does not come back if the button remains pressed.

I have also tried to have this:

Sub rf_inc_down

pressed = True
Do While pressed = True
If old_repeat <> repeat Then
If selected_rf <= 95 Then
selected_rf = selected_rf + 5
rf_power_value.text = selected_rf & "%"
Play(11)
draw_rf_power
End If
old_repeat = repeat
End If
Loop

End Sub

Sub inc_dec_repeat_Tick
repeat = repeat + 1 ' increment repeat timer every 500 ms
End Sub

Sub rf_inc_up
pressed = False
End Sub

but it seems that even when I release the button, the up sub does not get called. The system gets stuck in the down sub and force closes.

How can I implement an auto repeat of the button_down every 500 ms?
 

ssg

Well-Known Member
Licensed User
Longtime User
give this piece of code a try, it uses a timer to repeat every 500ms:


B4X:
Sub Process_Globals
   Dim tmr As Timer
End Sub

Sub Globals
   Dim b As Button
   Dim l As EditText
   Dim ctr As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   b.Initialize("b")
   Activity.AddView(b, 0, 0, 50%x, 20%y)
   b.Text = "click me"
   
   l.Initialize("")
   Activity.AddView(l, 0, 30%y, 100%x, 20%y)
   
   If FirstTime Then
      tmr.Initialize("tmr", 500)
   End If
   
   ctr = 0
   l.Text = ctr
End Sub

Sub tmr_Tick
   ctr = ctr + 1
   l.Text = ctr
End Sub

Sub b_Down
   tmr.Enabled = True
End Sub

Sub b_Up
   tmr.Enabled = False
End Sub
 
Upvote 0
Top