B4J Code Snippet Keyboard Shortcuts

The following will allow you to assign keyboard shortcuts to Nodes.

Whatever the text of the label, inserting _ (underscore) before the desired key letter, will make that letter the shortcut key.
in the example code below the first shortcut is ALT-S and the second ALT-M (the text set in designer was but overwritten in the labaleFor routine)

label text = "_Alpha" --> shortcut key will be ALT A
label text = "A_lpha" --> shortcut key will be ALT L
label text = "Al_pha" --> shortcut key will be ALT P

It seems like duplicate shortcuts are allowed, it simply cycles through the controls with the same shortcut letter.

B4X:
    Dim l As Label
    Dim tf As TextField
    l.Initialize("")
    tf.Initialize("")
    labelFor(l,"_Shortcut",tf)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("labelfor")
    labelFor(label1,"_Magic",t1)
    MainForm.RootPane.AddNode(tf,70,10,200,20)
    MainForm.RootPane.AddNode(l,10,10,60,20)
    MainForm.Show
End Sub
Sub labelFor(lab As Label,shortcut As String, nod As Node)
    Dim jo1 As JavaObject = lab
    lab.Text = shortcut
    jo1.RunMethod("setLabelFor",Array(nod))
    jo1.RunMethod("setMnemonicParsing",Array(True))
End Sub
 
Last edited:
Top