B4J Question B4J: key listener does not work

peacemaker

Expert
Licensed User
Longtime User
HI, All

One my B4J app uses key listener as many times shown on the forum.

But now i have copy-pasted the code to another app - and it does not work fully - no event.
The same code as usual:

B4X:
AddKeyPressedListener(MainForm)
...

Sub AddKeyPressedListener(myf As Form)
    Dim r As Reflector
    r.Target = myf.RootPane
    r.AddEventHandler("Main_KeyPressed", "javafx.scene.input.KeyEvent.KEY_PRESSED")
    r.AddEventHandler("Main_KeyPressed", "javafx.scene.input.KeyEvent.KEY_RELEASED")
End Sub

Private Sub Main_KeyPressed_Event(e As Event)
    Dim KE As Reflector
    KE.Target = e 'e is a KeyEvent instance
    '    Dim KeyChar As String = KE.RunMethod("getCharacter")
    Dim KeyCode As String = KE.RunMethod("getCode")
    '    Dim KeyText As String = KE.RunMethod("getText")
    Dim EventType As String = KE.RunMethod("getEventType")
 
'    Log(EventType)
 
    Select EventType
    
        Case "KEY_PRESSED"
            Select KeyCode
                Case "ESCAPE"
                    Log("ESC")
                    'next my code
            
            
                Case "CONTROL"
                    KEY_CTRL_PRESSED = True
                Case "X"
                    If KEY_CTRL_PRESSED Then
                        Log("CTRL+X PRESSED")
                    
                    End If
            End Select
    
 
        Case "KEY_RELEASED"
            If KeyCode = "CONTROL" Then
                KEY_CTRL_PRESSED = False
            End If
        
        
    End Select
    e.Consume
End Sub

Tried and r.AddEventFilter with correct event sub name updating - also no help.
Libs JavaObject and Reflection are included OK, no any errors.
App uses also MenuBar and EventFilter for mouse events (later in the class), but if to remove them for test - no help anyway.

But no "_Event" triggered.
What trick may be forgotten from 1st app where works OK ?
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
I need to catch just ESCAPE, but no. But first app is OK :(

Hot-keys of the menubar works OK here!
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
Your Code works ESC was captured
B4X:
    Select EventType
      
        Case "KEY_PRESSED"
            Select KeyCode
                Case "ESCAPE"
                    Log("ESC")
                    'next my code
              
              
                Case "CONTROL"
                    Log("CTRL")
                    KEY_CTRL_PRESSED = True
                  
                Case "X"
                    Log("X")
                    If KEY_CTRL_PRESSED Then
                        Log("CTRL+X PRESSED")
                      
                    End If
            End Select
      
  
        Case "KEY_RELEASED"
            Log("KEY_RELEASED")
            If KeyCode = "CONTROL" Then
                KEY_CTRL_PRESSED = False
            End If
'         
          
    End Select
Logs
B4X:
CTRL
X
CTRL+X PRESSED
KEY_RELEASED
KEY_RELEASED
ESC
KEY_RELEASED
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Yes, my first app is also working OK. But how to understand what the difference is in working app and this new trouble app...
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Only keep this code in new app, does it work?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
My trouble project after reducing is attached - pls, try ESCAPE key pressing.
The project was started by B4Jv.10.00\File\New\B4XPages
 

Attachments

  • PCBcomparer2_0.23.zip
    12.4 KB · Views: 18
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
The project was started by B4Jv.10.00\File\New\B4XPages
If to create a new project now via the same B4Jv.10.00\File\New\B4XPages and copy the key listener code - IT WORKS !
But what is wrong with my project now ? :confused:
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
HA ! If layout is without any visible and enabled button - key listener does not work ! :)
 
Upvote 0

teddybear

Well-Known Member
Licensed User
It seems, i found, there is some difference in the layout files, attached. The content is different, but visually it is just default "Button1" on the layout.
When project used "BYGGY_MainPage.bjl" layout - the key listener did not work.
But when i replaced this .bjl file by default "MainPage.bjl" AND RELOADED THE PROJECT - the project started to work....

But how to fix it - it seems, some internal details of the layout file that not editable visually in the Designer, @Erel.
You need a visual inputable componet like button, textfield etc when add listener for RootPane,otherwise it doesn't work.
To fix it, you can add keyevent handler with inline java for the scene.
Try this project
 

Attachments

  • PCBcomparer2.zip
    12.3 KB · Views: 15
Upvote 0
Top