B4J Question [B4XPages] Key event - KEY_PRESSED and KEY_RELEASED

Elric

Well-Known Member
Licensed User
Hi,

I'd like to manage the key event in a B4XPage but I don't know how.

It seems that forum return only result for B4A or for B4J using form.

Any hint?

Thanks!
 

TILogistic

Expert
Licensed User
Longtime User
I have a class for this, but I can't find it.

But look at this example:

and add this if you want to expand the example class.

 

Attachments

  • signin_test2.zip
    5.8 KB · Views: 191
Upvote 1

Elric

Well-Known Member
Licensed User
Can I use this to select a control?

For instance, can I use NUMPAD1 to select radiobutton1?

I'm trying without success...
 
Upvote 0

Elric

Well-Known Member
Licensed User
I'm interested to both of them.

At present, I'd like to use "UP" and "DOWN" to set focus, case by case, between 4 radiobuttons and use "ENTER" to checked one of them.
:)
 
Upvote 0

Elric

Well-Known Member
Licensed User
I've implemented this solution but moving the subs from oparra's class into b4xpage module.

I don't know how to manage it with the oparra's class!

Each B4XRadioButton need to have the tag set. Here the tag are the same of the name (B4XRadioButton1 etc.).
B4X:
Sub Class_Globals
    Private intrdbSelected As Int
End Sub

Sub AddKeyPressedListener
    Dim r As Reflector
    r.Target = Root
    r.AddEventFilter("keypressed", "javafx.scene.input.KeyEvent.ANY")
End Sub

Sub KeyPressed_Filter (e As Event)
  
    Dim jo As JavaObject = e
    Dim EventType As String = jo.RunMethodJO("getEventType", Null).RunMethod("getName", Null)
    If EventType = "KEY_PRESSED" Then
        Dim keycode As String = jo.RunMethod("getCode", Null)
          
        Select keycode
            Case "UP"
                intrdbSelected = intrdbSelected - 1             'intrdbSelected is a global variable to memorize the present position of selector cursor
                If intrdbSelected <= 0 Or intrdbSelected >= 5 Then
                    intrdbSelected = 4
                End If
                Wait For (GetTagOfB4XRadioButtonToBeSelected(intrdbSelected)) Complete (myResultString As String)
                rdbSelect(myResultString)
            Case "DOWN"
                intrdbSelected = intrdbSelected + 1
                If intrdbSelected <= 1 Or intrdbSelected >= 5 Then
                    intrdbSelected = 1
                End If
                Wait For (GetTagOfB4XRadioButtonToBeSelected(intrdbSelected)) Complete (myResultString As String)
                rdbSelect(myResultString)
        End Select
    End If
End Sub

Private Sub GetTagOfB4XRadioButtonToBeSelected(myInt As Int) As ResumableSub
'With this sub we get the tag of B4XRadioButton that will be selected

    Private myString As String
   
    Try
        If 1 = myInt Then
            myString = "B4XRadioButton1"
        Else If 2 = myInt Then
            myString = "B4XRadioButton2"
        Else If 3 = myInt Then
            myString = "B4XRadioButton3"
        Else If 4 = myInt Then
            myString = "B4XRadioButton4"
        End If
    Catch
        Log(LastException)
    End Try
   
    Return myString
End Sub

Private Sub rdbSelect(myString As String)
    DecolorizeRadioButtons
    For Each vw As B4XView In Root.GetAllViewsRecursive
        If vw.tag Is B4XRadioButton Then
            Private rdb As B4XRadioButton = vw.Tag
            Try
                If rdb.Tag = myString  Then
                    rdb.mBase.SetColorAndBorder(xui.Color_Transparent, 5dip, xui.Color_White, 5dip)
                End If
            Catch
                Log(LastException)
            End Try
        End If
    Next
End Sub

Private Sub DecolorizeRadioButtons
   
        B4XRadioButton1.mBase.SetColorAndBorder(xui.Color_Transparent, 1dip, xui.Color_LightGray, 5dip)
        B4XRadioButton2.mBase.SetColorAndBorder(xui.Color_Transparent, 1dip, xui.Color_LightGray, 5dip)
        B4XRadioButton3.mBase.SetColorAndBorder(xui.Color_Transparent, 1dip, xui.Color_LightGray, 5dip)
        B4XRadioButton4.mBase.SetColorAndBorder(xui.Color_Transparent, 1dip, xui.Color_LightGray, 5dip)
   
End Sub
 
Upvote 0
Top