B4J Question Capture KeyPress

teddybear

Well-Known Member
Licensed User
How can I capture KeyPress as in B4A with the event Activity_KeyPress?
Search the key keypress in the forums, you will get the answer which you asked.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Sorry, can't find that Sub in Simple Layout Designer v5
Why not use GameViewHelper as Erel said? it is easy to use.
B4X:
    Private gvh As GameViewHelper
    gvh.AddKeyListener("GVH", MainForm)
    
Sub GVH_KeyPressed (KeyCode As String) As Boolean
    Log(KeyCode)
    Return True
End Sub

Sub GVH_KeyReleased (KeyCode As String) As Boolean
    Log(KeyCode)
    Return True
End Sub
 
Upvote 0

PaulMeuris

Active Member
Licensed User
Sorry, can't find that Sub in Simple Layout Designer v5
That is because you didn't go to message #31 where you can find the last version v12.
1680662917751.png
 
Upvote 0

GuidoVL

Member
Why not use GameViewHelper as Erel said? it is easy to use.
B4X:
    Private gvh As GameViewHelper
    gvh.AddKeyListener("GVH", MainForm)
   
Sub GVH_KeyPressed (KeyCode As String) As Boolean
    Log(KeyCode)
    Return True
End Sub

Sub GVH_KeyReleased (KeyCode As String) As Boolean
    Log(KeyCode)
    Return True
End Sub
How can I use this with B4XPages? Putting this as MainForm says "Types do not match".
 
Upvote 0

teddybear

Well-Known Member
Licensed User
How can I use this with B4XPages? Putting this as MainForm says "Types do not match".

With B4xpages use B4xPages.GetNativeParent instead

B4X:
gvh.AddKeyListener("GVH", B4xPages.GetNativeParent(me))
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I guess what you want to capture is closeRequest event, if it is ture, the below code maybe help you.

B4X:
Private Sub B4XPage_CloseRequest As ResumableSub
    Dim sf As Object = xui.Msgbox2Async("Close?", "Title", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Return True
    End If
    Return False
End Sub
 
Upvote 0

GuidoVL

Member
I guess what you want to capture is closeRequest event, if it is ture, the below code maybe help you.

B4X:
Private Sub B4XPage_CloseRequest As ResumableSub
    Dim sf As Object = xui.Msgbox2Async("Close?", "Title", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Return True
    End If
    Return False
End Sub
That's it! Thanks a lot.
 
Upvote 0
Top