See if home key was pressed?

grant1842

Active Member
Licensed User
Longtime User
I'm making a game and if the activity is left by the user (back or home key pressed), the activity needs to end the game by posting to a script and ending the activity.

I can detect if the back key is pressed, however, I cannot get any valid method to detect if the home key is pressed. I can't just end the game in the Activity_Pause method because let's say the user receives a phone call mid-game.

The Activity_Pause's UserClosed method returns false if home key is pressed. I tried using this code but no luck. Note that I'm not wanting to trap the home key event, just want to know if it was pressed when the Activity is paused.
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
   If KeyCode = KeyCodes.KEYCODE_HOME Then
      Log("home")
      awake.ReleaseKeepAlive
                'Post to script to end game
      Return False
   End If
End Sub
 
Last edited:

grant1842

Active Member
Licensed User
Longtime User
If I put it under Activity_Pause and a user receives a phone call this would remove them from the game because the Activity is paused.

I understand you can't trap the event, however, has anyone found a way to see if the activity was left by the user instead of something else like a phone call sending it to the background.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You could set a flag in Process_Globals and set it to TRUE or whatever if the BACK key is pressed, pressing the HOME key will not set it, therefore, you will know if the user closed the app or pressed home or the app was interrupted by a phone call, etc. and do what you need to do in Activity_Pause.
 
Upvote 0

grant1842

Active Member
Licensed User
Longtime User
NJDude, I might be a little confused. I'm not seeing how what you suggested will help me be able to see if the activity was left by the home key. I need to remove the player from the game ONLY if they left the activity with a back key press or pressing home key. Unless I'm confused, it sounds like that method would fire even if a phone call was received.

If you could elaborate I would really appreciate it.

Thanks.
 
Upvote 0

grant1842

Active Member
Licensed User
Longtime User
Turns out that the Activity_KeyPress isn't even being fired when the Home key is pressed. So I still have the issue of knowing whether or not the home key was pressed or if the Activity got sent to the background from something like a phone call.
 
Last edited:
Upvote 0

grant1842

Active Member
Licensed User
Longtime User
Thanks I do appreciate it. However, the player would be removed from the game if the home key was pressed, as well as if let's say the player received a phone call. In short, I only want to remove the player if they pressed the back key or the home key, and not any other circumstances.
 
Upvote 0
Top