B4J Question [SOLVED] Reading keyevents...

Magma

Expert
Licensed User
Longtime User
As old vb6-programmer missed tabstop and i want to use enter like tab key too...

So found this to trap keyevents...

but misses...
How to check what pressed...
B4X:
    Dim KeyEvent As JavaObject = Args(0)
'if i log(keyevent) took that code=enter / but keyevent is an object...

When i am try to set keyevent in a map... get error that can not assigned...

How i ll check keyevent.code="ENTER" so nextfield.requestfocus or do a job ?
 

Magma

Expert
Licensed User
Longtime User
Ok i can read... the keycode.. but how i will do that if i have many textfields and i want to check them all...

this code not worked for me (ofcourse with events subs for all my textfields):

B4X:
    setHandler(Pane1,"setOnKeyPressed","textfield1_keypressed")
    setHandler(Pane1,"setOnKeyPressed","textfield2_keypressed")
    setHandler(Pane1,"setOnKeyPressed","textfield3_keypressed")

the code down here works for one textfield:

B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim tf As TextField
Dim keycode As JavaObject
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
tf.Initialize("tf1")
keycode.InitializeStatic("javafx.scene.input.KeyCode")
MainForm.RootPane.AddNode(tf,10,10,100,20)
setHandler(tf,"setOnKeyPressed","tf_KeyPress")
End Sub
Sub tf_KeyPress_Event(MethodName As String, Args() As Object)
Log(Args(0))
If asJO(Args(0)).RunMethod("getCode",Null) == keycode.GetField("UP") Then
' uppercase the text if 'up' is pressed
tf.text = tf.Text.ToUpperCase
asJO(tf).RunMethod("end",Null) ' make cursor stay at end of field
Log("up arrow pressed")
asJO(Args(0)).RunMethod("consume",Null)' eat the event to stop default behaviour
End If
End Sub
Sub asJO(o As JavaObject) As JavaObject
Return o
End Sub
Sub setHandler(ob As JavaObject,eventName As String,handlerName As String)
ob.RunMethod(eventName, Array(ob.CreateEventFromUI("javafx.event.EventHandler",handlerName,False)))
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Because you are setting the eventhandler on the pane for each one, you need to set it for each textfield.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You could use the same sub for all textfields if it's simpler.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim tf,tf1,tf2 As TextField
    Dim keycode As JavaObject
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    tf.Initialize("tf")
    tf.Tag = "tf"
    tf1.Initialize("tf1")
    tf1.Tag = "tf1"
    tf2.Initialize("tf2")
    tf2.Tag = "tf2"
    keycode.InitializeStatic("javafx.scene.input.KeyCode")
    MainForm.RootPane.AddNode(tf,10,10,100,20)
    MainForm.RootPane.AddNode(tf1,10,40,100,20)
    MainForm.RootPane.AddNode(tf2,10,80,100,20)
    setHandler(tf,"setOnKeyPressed","tf_KeyPress")
    setHandler(tf1,"setOnKeyPressed","tf_KeyPress")
    setHandler(tf2,"setOnKeyPressed","tf_KeyPress")
End Sub
Sub tf_KeyPress_Event(MethodName As String, Args() As Object)
    Log(Args(0))
    Dim target As TextField = asJO(Args(0)).RunMethod("getTarget",Null)
    Log("tf tag " & target.tag)
    If asJO(Args(0)).RunMethod("getCode",Null) == keycode.GetField("UP") Then
        ' uppercase the text if 'up' is pressed
        tf.text = tf.Text.ToUpperCase
        asJO(tf).RunMethod("end",Null) ' make cursor stay at end of field
        Log("up arrow pressed")
        asJO(Args(0)).RunMethod("consume",Null)' eat the event to stop default behaviour
    End If
End Sub
Sub asJO(o As JavaObject) As JavaObject
    Return o
End Sub
Sub setHandler(ob As JavaObject,eventName As String,handlerName As String)
    ob.RunMethod(eventName, Array(ob.CreateEventFromUI("javafx.event.EventHandler",handlerName,False)))
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…