B4J Question How to detect the user has pressed the Enter key into a TextField? [solved]

Didier9

Well-Known Member
Licensed User
Longtime User
Message title tells it all...
I have not been able to find a substitute or equivalent for the VB InputBox.
It would be very helpful if I could detect that the user has pressed the Enter key, indicating that he is done with entering a text string and that I can proceed with validation.
 

Didier9

Well-Known Member
Licensed User
Longtime User
Yes it is, thanks.
I had checked it but it does not actually return the Enter key (or carriage return) but it seems that it's the only event that fires when enter is pressed, so it will do the job.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Yes it is, thanks.
I had checked it but it does not actually return the Enter key (or carriage return) but it seems that it's the only event that fires when enter is pressed, so it will do the job.

Usually (if not always) people use the Tab key when done with a TBox
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Obviously not always since I personally never do except in cases where Enter does not work like in multi-line boxes where it inserts a carriage return (how rude...).
Tab has a number of secondary functionality that is app dependent and not well codified (including, sometimes, who would have guessed, inserting a Tab...). Enter key in a single line field typically signifies that you are done with the field.
But it is a good suggestion to check what happens with the Tab key.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Yes it is, thanks.
I had checked it but it does not actually return the Enter key (or carriage return) but it seems that it's the only event that fires when enter is pressed, so it will do the job.
You might want to try the following, which I am using in a class where et is the textfield view. Set this up in the appropriate place ( in my case this code runs in the DesignerCreateView sub):
B4X:
Dim r As Reflector
    r.Target = et
    r.AddEventFilter("et", "javafx.scene.input.KeyEvent.KEY_PRESSED")
    r.AddEventFilter("et", "javafx.scene.input.KeyEvent.KEY_RELEASED")
and then use this sub:
B4X:
Sub et_Filter (EventData As Event)
    Dim jo As JavaObject = EventData
    Dim code As String = jo.RunMethod("getCode", Null)
    Dim EventType As String = jo.RunMethod("getEventType", Null)
    ' Log(code)
    ' Log(EventType)
    If EventType = "KEY_PRESSED" Then
              ... some code
            ' Possibly this, depending on what you are doing
            EventData.Consume
        else if code = "ENTER" Then
            ... some code
            ' Possibly this, depending on what you are doing
            EventData.Consume
        else if code = "ESCAPE" Then
            ... some code
            ' Possibly this, depending on what you are doing
            EventData.Consume
        else if code = "BACK_SPACE" Then
            If et.Text = "" And Expanded Then Shrink
        End If
    else if EventType = "KEY_RELEASED" Then
            ... some code
            ' Possibly this, depending on what you are doing
            EventData.Consume
    End If
End Sub
For the code I am running I do NOT consumer the event, but I think it may depend on circumstances.
This gives you tons of flexibility, and mimics code I used to use in VB6. Add different Keys as you need them.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Thank you Guy, I am sure that code will be useful, I have a fair amount of VB 6.0 code to convert...
 
  • Like
Reactions: LGS
Upvote 0
Top