Android Question Is SoftButtonBack reliably identical to HardButtonBack

nikolaus

Member
Licensed User
Longtime User
Android Button Blues:

I am catching KeyPress.Keycode_BACK and split it into short and long keypress by autorepeat-count. That is working fine on Galaxy Note 1 and Note 3, both with hardware BackButton.

In the emulator the long keypress is not working. Is this an issue with the emulator or is there a different behaviour between hard and soft buttons (i.e. no autorepeat)?

I might seperate short and longpress with a timer as well but that would make sense only if I could expect consistent behaviour then and acceptable likeliness in the future.

Or should I leave all system buttons either hard or soft as is and not use them?

Thanks
Nikolaus
 

nikolaus

Member
Licensed User
Longtime User
Erel, thanks for answering. The relevant code is:

KeyBackPressed and LongKeypressTheshold are Globals.

B4X:
Sub Activity_KeyPress (KeyPress As Int) As Boolean : Log("Activity_Keypress")
   
    Select KeyPress
   
        Case KeyCodes.KEYCODE_BACK
       
            KeyBackPressed = KeyBackPressed + 1    
           
            If KeyBackPressed = LongKeypressTheshold Then   
                NavigateForward                                   
            End If
   
            Return True

B4X:
Sub Activity_KeyUp (KeyUp As Int) As Boolean : Log("Activity_KeyUp")
   
    Select KeyUp
   
        Case KeyCodes.KEYCODE_BACK
           
            If KeyBackPressed < LongKeypressTheshold Then 
                NavigateBack                   
            End If
            KeyBackPressed = 0
            Return True
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I wouldn't rely on this value. You can instead store the current time in a global variable and then check the duration in KeyUp:
B4X:
Sub Activity_KeyPress
 ...
 StartTime = DateTime.Now 'StartTime is a Long variable

End Sub

Sub Activity_KeyUp
 ...
 If DateTime.Now - StartTime > 500 Then '500 milliseconds
 End If
End Sub
 
Upvote 0

nikolaus

Member
Licensed User
Longtime User
I implemented it that way first but there were two problems with that approach.

1. If the user knows short keypress => action A and long keypress => action B then he will press and release the key to get A but press the key and wait to get B. That means the long keypress needs to be detected within Activity_KeyPress but has to be interupted by KeyUp.

I found a solution for that but it was not very elegant and in rare cases failed during 'really stupid user tests'. I had not found a reliable solution.

2. At least on my Galaxy Note 1 and Note 3 the hardware button fires around every 80ms on long keypress and any device with hardware buttons should do so. So the repeated keypress has to be consumed until either KeyUp or LongKeypressThreshold interrupts. That led me to the simple and robust method of counting (autorepeated) keypress. Unfortunately at least in the emulator the soft buttons seem not to autorepeat on long keypress for whatever reason - or unreason :)

I think my question has been answered: I'll stay away from using hardware buttons with short and long keypress ..

thanks Erel for your great support this time and always
 
Upvote 0
Top