B4J Question AddEventHandler2 does not work if you do not have a button on the form?

BeneBarros

Active Member
Licensed User
Longtime User
AddEventHandler2 does not work if you do not have a button on the form?

Ex.:

This code works
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    btn1.Initialize("btn1")
    btn1.Text = "Test"
    MainForm.RootPane.AddNode(btn1, 20, 20, 100, 25)

    Dim Obj As Reflector
    Obj.Target = MainForm.RootPane
    Obj.AddEventHandler2("RootPaneKeyEvent", "onKeyReleasedProperty")
       
    MainForm.Show
End Sub

Sub RootPaneKeyEvent_Event(e As Event)
    ...
    ...
End Sub

This code does not work
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    Dim Obj As Reflector
    Obj.Target = MainForm.RootPane
    Obj.AddEventHandler2("RootPaneKeyEvent", "onKeyReleasedProperty")
       
    MainForm.Show
End Sub

Sub RootPaneKeyEvent_Event(e As Event)
    ...
    ...
End Sub
 

Daestrum

Expert
Licensed User
Longtime User
Just add
B4X:
MainForm.RootPane.RequestFocus
after the MainForm.Show

Probably caused by having no child nodes that automatically request focus.
 
Upvote 0
Top