Android Question How to trap Escape key

Discussion in 'Android Questions' started by FERNANDO SILVEIRA, Jun 6, 2018.

  1. FERNANDO SILVEIRA

    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
     
  2. PassionDEV

    PassionDEV Active 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
    Code:
    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: Jun 7, 2018
    FERNANDO SILVEIRA likes this.
  3. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Yes... the back button
     
  4. PassionDEV

    PassionDEV Active Member Licensed User

    check my edit of my post

    return true will pause the termination of the activity

    return false will close
     
    FERNANDO SILVEIRA likes this.
  5. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    That's it

    Thank you very much.
     
    PassionDEV likes this.
  6. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    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?

    Code:
    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
     
  7. PassionDEV

    PassionDEV Active Member Licensed User

    i would do it like this
    Code:
    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
     
    FERNANDO SILVEIRA likes this.
  8. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Now it is working, PassionDEV. Thanx!!! ;)
    I didn't know about this: Return True / Return False :(
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice