Android Question B4XPages - Intercepting KeyPress

epiCode

Active Member
Licensed User
How do I Implement the following code in B4XPages:

In Activity Main:
Sub Activity_KeyPress (KeyCode As Int) As Boolean

Select Keycode
    Case KeyCodes.KEYCODE_BACK
        'code to handle back key
    Case KeyCodes.KEYCODE_VOLUME_DOWN, KeyCodes.KEYCODE_VOLUME_UP
        'code to handle volume keys
    Case KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE
        'code to handle media button
    Case Else
        Return False 'Pass to Android System
End Select
 
Solution
Change the Activity_KeyPress code to:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
    End If
    B4XPages.GetManager.RaiseEvent(B4XPages.GetManager.GetTopPage, "B4XPage_KeyPress", Array(KeyCode))
    Return False
End Sub
And implement B4XPage_KeyPress in the relevant pages.
The back key will be handled in the CloseRequest event sub.

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change the Activity_KeyPress code to:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
    End If
    B4XPages.GetManager.RaiseEvent(B4XPages.GetManager.GetTopPage, "B4XPage_KeyPress", Array(KeyCode))
    Return False
End Sub
And implement B4XPage_KeyPress in the relevant pages.
The back key will be handled in the CloseRequest event sub.
 
Upvote 0
Solution

epiCode

Active Member
Licensed User
It worked. Solution Accepted. Closed!!!!
Thanks Erel
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…