How to simulate a back key press?

ztigr

New Member
Licensed User
Longtime User
I know how to capture it, but is there anyway to simulate a user pushing the back key? You guys are great.

Ztigr
 

Mahares

Expert
Licensed User
Longtime User
You still need the below sub or similar to perform Margret's code snippet. Please correct me if you disagree.

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    Dim Selection As Short
    If KeyCode = KeyCodes.KEYCODE_BACK Then    
       Selection = Msgbox2("Exit application?".ToUpperCase, "C o n f i r m a t i o n", "Yes", "", "No", Null)       
       Select Selection   
              Case DialogResponse.POSITIVE              
                   Activity.Finish                   
              Case DialogResponse.CANCEL, DialogResponse.NEGATIVE 
                   Return True                      
       End Select   
    End If   
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You still need the below sub or similar to perform Margret's code snippet. Please correct me if you disagree.

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    Dim Selection As Short
    If KeyCode = KeyCodes.KEYCODE_BACK Then    
       Selection = Msgbox2("Exit application?".ToUpperCase, "C o n f i r m a t i o n", "Yes", "", "No", Null)       
       Select Selection   
              Case DialogResponse.POSITIVE              
                   Activity.Finish                   
              Case DialogResponse.CANCEL, DialogResponse.NEGATIVE 
                   Return True                      
       End Select   
    End If   
End Sub

In the case of a single activity, this holds true. In the case of returning to a 'previous' activity, perhaps no :)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
So, what else do you need besides the code snippet you posted as clicking the button returns the following error:
Error description: Undeclared variable 'activity_keypress' is used before it was assigned any value.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
So, what else do you need besides the code snippet you posted as clicking the button returns the following error:
Error description: Undeclared variable 'activity_keypress' is used before it was assigned any value.

Certainly this error will occur when sub activity_keypress is missing, which, however, will not be the case, when someone traps the event, as already mentioned by the author of this topic :)
 
Upvote 0

desof

Well-Known Member
Licensed User
Longtime User
B4X:
Sub But1_Click  'Simulate backkey press
      Dim KC As Int
      KC = KeyCodes.KEYCODE_BACK
      Activity_KeyPress (KC)
End Sub

Hello!

sorry for my bad English. I'm doing this and it does not work.
I want you to press the menu button behaves in the same way as pressing the phone button


B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim ph As Phone
Dim bd As BitmapDrawable
   
Activity.LoadLayout("layout")
ph.SetScreenOrientation (1)   

If FirstTime=True Then 
   Ventana
   GrabaTExto
End If
'''
bd = ph.GetResourceDrawable(17301569)
Activity.AddMenuItem2 ("Acerca de..","Button3", bd.Bitmap)      
bd = ph.GetResourceDrawable(17301560)
Activity.AddMenuItem2("Salir","Button4", bd.Bitmap)   

End Sub

Sub Activity_KeyPress(KeyCode As Int) As Boolean

If KeyCode = KeyCodes.KEYCODE_BACK Then
   
Dim ph As Phone
   Dim bd As BitmapDrawable
   bd = ph.GetResourceDrawable(17301659)
   tmpValue="Desea salir de la aplicación?: "
   result=Msgbox2(tmpValue,"","Aceptar","Cancelar","",bd.Bitmap)

   If result=DialogResponse.POSITIVE Then
       Activity.Finish
   Else
      Return True
   End If
Else If KeyCode = KeyCodes.KEYCODE_MENU Then
   'nothing   
Else
    Return True   ' con esto vuelves al programa, si no pones nada sale de el   
End If

End Sub

Sub bMenu_Click
      Dim KC As Int
      KC = KeyCodes.KEYCODE_MENU
      Activity_KeyPress (KC)      
End Sub
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Hi mc73,
In the case of a single activity, this holds true. In the case of returning to a 'previous' activity, perhaps no :)

Please to explain your words addition. I'm interested in your words. Could you show me a example?
 
Last edited:
Upvote 0
Top