B4J Code Snippet KeyBoard Event,detect CTRL+X or etc...

B4X:
Private KEY_CTRL_PRESSED As Boolean = False

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
    
    AddKeyPressedListener(MainForm)
End Sub

Sub AddKeyPressedListener(f As Form)
    f.RootPane.RequestFocus
    Dim r As Reflector
    r.Target = f.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")
                
                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
 

YPMN

Member
Licensed User
Longtime User
hi behnam_tr, congratulations for your interesting work which was very useful to me. But then after adapting it to B4XPages, nothing works anymore, I'm lost. I suspect replacing:

B4X:
AddKeyPressedListener(MainForm)

in:

B4X:
AddKeyPressedListener(B4XPages.GetPage("B4XMainPage"))

How to adapt this line of code in B4XPages?
 

behnam_tr

Active Member
Licensed User
Longtime User
in b4j :
use codes in mainform

tested and works .
 

Attachments

  • Splash.zip
    289.2 KB · Views: 311

YPMN

Member
Licensed User
Longtime User
Behnam_Tr, thanks very much! This example now allows me to correct and improve my understanding of how B4XPages works.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…