B4J Question How to get keycode when key pressed?

bjfhs

Active Member
Licensed User
I can't get keycode.
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    AddKeyPressedListener
End Sub
Sub AddKeyPressedListener
    Dim r As Reflector
    r.Target = MainForm.RootPane
    r.AddEventHandler("keypressed", "javafx.scene.input.KeyEvent.KEY_PRESSED")
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
Sub KeyPressed_Event (e As Event)
    Log("d")
    Dim jo As JavaObject = e
    Dim keycode As String = jo.RunMethod("getCode", Null)
    If keycode = "ESCAPE" Or keycode = "C" Then
        ExitApplication
    Else If keycode = "A" Then
           
    Else If keycode = "N" Then
           
    End If
End Sub
 

Daestrum

Well-Known Member
Licensed User
You need to add at least one control to the rootpane or else have Mainform.RootPane.RequestFocus (after Mainform.Show ), or it will never receive focus and be available for input.
 
Last edited:
Top