Android Question How to trap Escape key

FERNANDO SILVEIRA

Active Member
Licensed User
Hello guys,

I have a pop up panel within the Activity so user can view additional information.
Which event must be monitored in order to prevent a Escape key press of ending the program?




Regards,
Fernando
 

Addo

Well-Known Member
Licensed User
do you mean you want to catch the back button press on phone ?

if yes then you can use Activity key press
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
'do what ever as return

End if   
End Sub
 
Last edited:
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
check my edit of my post

return true will pause the termination of the activity

return false will close

Hello PassionDEV, I adapted the trap code as below, but the program is ending doesn't matter which option I pick on Msgbox2... Note that Activity.Finish is commented...

What am I doing wrong?

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Dim result As Int
        result = Msgbox2("Deseja realmente sair?", "Confirme", "Sim", "", "Não", Null)
        Log("Result = " & result)
        If result = DialogResponse.Positive Then
'                Activity.Finish
        End If
    End If
End Sub
 
Upvote 0

Addo

Well-Known Member
Licensed User
i would do it like this
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
'do what ever as return
closeconfirm
Return True
Else
Return False
End If                                        
End Sub

Sub  closeconfirm
Dim result As Int
result = Msgbox2("Deseja realmente sair?", "Confirme", "Sim", "", "Não", Null)
Select result
Case DialogResponse.POSITIVE
Activity.Finish
Case DialogResponse.CANCEL
closeconfirm
End Select
       
End Sub

not tested i have just write it in forum code editor
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
i would do it like this
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
'do what ever as return
closeconfirm
Return True
Else
Return False
End If                                       
End Sub

Sub  closeconfirm
Dim result As Int
result = Msgbox2("Deseja realmente sair?", "Confirme", "Sim", "", "Não", Null)
Select result
Case DialogResponse.POSITIVE
Activity.Finish
Case DialogResponse.CANCEL
closeconfirm
End Select
      
End Sub

not tested i have just write it in forum code editor

Now it is working, PassionDEV. Thanx!!! ;)
I didn't know about this: Return True / Return False :(
 
Upvote 0
Top