### Handle KeyPress Events

This example shows how to handle special key handlers for a TextField.
For a TextField tfEventText the KeyPressed and KeyReleased Events are handled.
Handled are the keys
T = Set the actual time in the TextField
D = Set the actual date in the textfield
Define the textfield
`Sub Process_Globals
...
Private tfEventTest As TextField
End Sub`
Create the setonkeypressed and released events
`Sub AppStart (Form1 As Form, Args() As String)
...
'Create Key pressed and released events to capture key presses and handle special chars
'Assign the events to the textfield Private tfEventTest As TextField
tfEventTest.TooltipText = "Enter T for the actual time, D for actual date"
Dim joTF As JavaObject = tfEventTest
Dim oTF As Object = joTF.CreateEvent("javafx.event.EventHandler","TextFieldKeyPressed",False)
joTF.RunMethod("setOnKeyPressed",Array(oTF))
Dim oTF As Object = joTF.CreateEvent("javafx.event.EventHandler","TextFieldKeyReleased",False)
joTF.RunMethod("setOnKeyReleased",Array(oTF))
End Sub`
Create the key event handlers
'For textfield tfEventText handle keypressed and keyreleased events
'Handled are the keys
'T = Set the actual time in the TextField
'D = Set the actual date in the textfield
'Handle keypressed event
`Private Sub TextFieldKeyPressed_Event(EventName As String,Args() As Object) As Object
Dim KEvent As Event = Args(0)
Dim KEventJO As JavaObject = Args(0)
Dim KeyName As String = KEventJO.RunMethodJO("getCode",Null).RunMethod("getName",Null)
Select KeyName.ToUpperCase
Case "T", "D"
'Set the Textfield non editable so the text cursor and the char are not displayed
Dim JO As JavaObject = tfEventTest
JO.RunMethod("setEditable",Array(False))
'Consume the event
KEvent.Consume
End Select
Return False
End Sub`
'Handle keyreleased event
`Private Sub TextFieldKeyReleased_Event(EventName As String,Args() As Object) As Object
Dim KEvent As Event = Args(0)
Dim KEventJO As JavaObject = Args(0)
Dim KeyName As String = KEventJO.RunMethodJO("getCode",Null).RunMethod("getName",Null)
Select KeyName.ToUpperCase
'Set the actual time in the textfield
Case "T"
Dim JO As JavaObject = tfEventTest
JO.RunMethod("setText",Array(DateTime.Time(DateTime.Now)))
'Send the focus to the textfield
tfEventTest.RequestFocus
'Consume the event
KEvent.Consume
'Set the actual date in the textfield
Case "D"
Dim JO As JavaObject = tfEventTest
JO.RunMethod("setText",Array(DateTime.Date(DateTime.Now)))
JO.RunMethod("setEditable",Array(True))
'Send the focus to the textfield
tfEventTest.RequestFocus
'Consume the event
KEvent.Consume
End Select
Return False
End Sub`

Changed: 2018-05-17 18:34, Category: TextField, File: T6C262M1939.htm, ID: 1939