1. Tap menu Exit on screen.
2. Press back button on device.
But I only want to accept the "menu way" not "button way". I try to catch KeyPress event but I don't know how. Documentation doesn't mention about it too.
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
If panelSearch.Visible=True Then
panelSearch.Visible=False
Return True
End If
res = Msgbox2("Do you want to exit?", "", "Yes", "", "No", Null)
If res = DialogResponse.NEGATIVE Then Return True
End If
Return False
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
If keycode = KeyCodes.KEYCODE_BACK Then
Return True
Else
Return False
End If
End Sub
I want activity ignores back button, home button and only close after user press menu button 3 times. My code below:
B4X:
Sub Globals
Dim menucount As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
menucount = 0
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If Keycode = KeyCodes.KEYCODE_BACK Then
Return True
End If
If Keycode = KeyCodes.KEYCODE_MENU Then
Return True
End If
If Keycode = KeyCodes.KEYCODE_MENU Then
menucount = menucount + 1
If menucount = 3 Then
Activity.Finish
End If
End If
End Sub
It compiled ok but it doesn't work as I want. It ignores back button not home button and nothing happen when I press menu button 3 times. Something's wrong here? :sign0085:
The Home button cannot be caught. This is a "feature" of the OS. I'm not sure about the Menu button. But it is likely that it does not get caught as well.
If you look at your code!
In the 2nd and in the 3rd If statement you test the same KeyCode !
Replace your code by :
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
If Keycode = KeyCodes.KEYCODE_BACK Then
Return True
End If
[COLOR=red]If Keycode = KeyCodes.KEYCODE_HOME[/COLOR] Then
Return True
End If
If Keycode = KeyCodes.KEYCODE_MENU Then
menucount = menucount + 1
If menucount = 3 Then
Activity.Finish
End If
End If
End Sub