Android Question [B4X] B4XPages - how to catch volume key?

stevenindon

Active Member
Licensed User
Hello all,

First time using B4XPage... how can we catch the volume button in B4XPage like we used to be

My Old example :
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    Select Case KeyCode
        Case 4 'BACK KEY
            RaiseUserEvent("ExecReturn_Keycode_Key","BACK")
        Case 24 'VOLUME UP
            RaiseUserEvent("ExecReturn_Keycode_Key","VOLUP")
        Case 25 'VOLUME DOWN
            RaiseUserEvent("ExecReturn_Keycode_Key","VOLDOWN")
    End Select
    Return True
End Sub

I am able to catch 'back' key using Sub B4XPage_CloseRequest As ResumableSub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Untested code - change Activity_KeyPress in Main module to:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If B4XPages.Delegate.Activity_KeyPress(KeyCode) Then Return True
    B4XPages.GetManager.RaiseEvent(B4XPages.GetManager.GetTopPage, "B4XPage_KeyPress", Array(KeyCode))
    Return True
End Sub

Handle it with:
B4X:
Private Sub B4XPage_KeyPress (Keycode As Int)

End Sub
 
Upvote 1

stevenindon

Active Member
Licensed User
Erel,

Thanks for your reply, I did a couple of test... and here are the results :

If i put in Main : It works..

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    msgbox(KeyCode,"")
    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

but if i put in B4XPages : It doesnt work.
B4X:
Private Sub B4XPage_KeyPress (Keycode As Int) as Boolean
    msgbox(KeyCode,"")
End Sub

Private Sub B4XPage_KeyPress (Keycode As Int) as Boolean in B4XPages event not raised.

Is there a bug.. or did i did something wrong.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
You need both. The Sub in Main calls the one in the B4XPage. That is what this line does
Return B4XPages.Delegate.Activity_KeyPress(KeyCode)

EDIT: I didn't read your code carefully, You need both Subs exactly as written by Erel above. The one in Main calls the B4XPage_KeyPress Sub in the top page by

B4XPages.GetManager.RaiseEvent(B4XPages.GetManager.GetTopPage, "B4XPage_KeyPress", Array(KeyCode))
 
Last edited:
Upvote 0
Top