B4J Question setMnemonicParsing for controls using FontAwesome Icons

xulihang

Active Member
Licensed User
Longtime User

stevel05

Expert
Licensed User
Longtime User
How would you imagine this working? To trigger the mnemonic you would need to enter the character to which it is attached. probably not possible with Fontawesome Icons.

It would be simpler to emulate the function with a keypressed listener.

You could still add the underscore to the icon in the designer and enable mnemonics to get the visual to work the same, but it wouldn't fire the event.


B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    Button1.As(JavaObject).RunMethod("setMnemonicParsing",Array(True))
    Dim O As Object = Button1.As(JavaObject).CreateEvent("javafx.event.EventHandler","BtnKeyPressed",Null) ' Requires CreateEvent not CreateEventFromUI if you want to consume the event.
    Button1.As(JavaObject).RunMethod("setOnKeyPressed",Array(O))
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
End Sub

Private Sub BtnKeyPressed_Event (MethodName As String, Args() As Object)
    Dim Event As JavaObject = Args(0)
    If Event.RunMethod("isAltDown",Null) Then  'AltDown to emulate mnemonic behaviour
        If Event.RunMethodJO("getCode",Null).RunMethod("toString",Null) = "E" Then
            Sender.As(JavaObject).RunMethod("fire",Null)
            Event.RunMethod("consume",Null)
        End If
    End If
 
End Sub

You could obviously add the Key 'E' in this case to the Tag value of the button to check if the correct button is pressed and make the sub reusable. And probably add the key required to the tooltip to make it obvious what is expected from the user.
 

Attachments

  • mnemonicparsingtest.zip
    2.2 KB · Views: 25
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The whole thing as a Designer Script Extension:

B4X:
'Parameters: 1 or more Buttons comma delimited
'Designer Script : {DSE_CLass}.SetKeyPressedEvent(Button1,Button2,Button3)
Public Sub SetKeyPressedEvent(DesignerArgs As DesignerArgs)
    If DesignerArgs.FirstRun Then
        For i = 0 To DesignerArgs.Arguments.Size - 1
            Dim V As JavaObject = DesignerArgs.GetViewFromArgs(i)
            V.RunMethod("setMnemonicParsing",Array(True))
            Dim O As Object = V.As(JavaObject).CreateEvent("javafx.event.EventHandler","BtnKeyPressed",Null)
            V.As(JavaObject).RunMethod("setOnKeyPressed",Array(O))
        Next
    End If
  
End Sub

Private Sub BtnKeyPressed_Event (MethodName As String, Args() As Object)
    Dim Event As JavaObject = Args(0)
    Dim Btn As Button = Sender
    If Event.RunMethod("isAltDown",Null) Then
        If Event.RunMethodJO("getCode",Null).RunMethod("toString",Null) = Btn.Tag Then
            Btn.As(JavaObject).RunMethod("fire",Null)
            Event.RunMethod("consume",Null)
        End If
    End If
  
End Sub
 

Attachments

  • mnemonicparsingtest_DSE.zip
    2.8 KB · Views: 21
Upvote 0

stevel05

Expert
Licensed User
Longtime User
While this works if the button has focus, it is not quite the same functionality as true mnemonics. Would have to dig a little deeper to get it to work properly I think. Let me know if it's on the right lines, perhaps we can sort it out if needed.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I think this is as close as I can get for now with the correct scope. It may give you a place to start.

B4X:
'Just to mimic the mnemonic visual
    Button1.As(JavaObject).RunMethod("setMnemonicParsing",Array(True))
    Button2.As(JavaObject).RunMethod("setMnemonicParsing",Array(True))
    Button3.As(JavaObject).RunMethod("setMnemonicParsing",Array(True))
    Button4.As(JavaObject).RunMethod("setMnemonicParsing",Array(True))
    
    ButtonMap.Initialize
    ButtonMap.Put(Button1.Tag,Button1)
    ButtonMap.Put(Button2.Tag,Button2)
    ButtonMap.Put(Button3.Tag,Button3)
    ButtonMap.Put(Button4.Tag,Button4)
    
    Dim O As Object = MainForm.RootPane.As(JavaObject).CreateEvent("javafx.event.EventHandler","BtnKeyPressed",Null)
    MainForm.RootPane.As(JavaObject).RunMethod("setOnKeyPressed",Array(O))
End Sub

Private Sub BtnKeyPressed_Event (MethodName As String, Args() As Object)
    Dim Event As JavaObject = Args(0)
    Dim Str As String = Event.RunMethodJO("getCode",Null).RunMethod("toString",Null)
    If Str = "ALT" Then Return
    If Event.RunMethod("isAltDown",Null) Then
        If ButtonMap.ContainsKey(Str) Then
            ButtonMap.Get(Str).As(JavaObject).RunMethod("fire",Null)
            Event.RunMethod("consume",Null)
        End If
    End If
End Sub
 

Attachments

  • mnemonicparsingtest_Scoped.zip
    3.4 KB · Views: 20
Upvote 1
Top