B4J Question How to assign hot key to button

Lahksman

Active Member
Licensed User
Longtime User
Reflection should work. I implemented it as following.

Add a handler
B4X:
Dim r As Reflector
r.Target = PaneGame
r.AddEventHandler("Pane_KeyPressed","javafx.scene.input.KeyEvent.KEY_PRESSED")

Consume the event
B4X:
Private Sub Pane_KeyPressed_Event(e As Event)
    keyListener(e)
End Sub

keylistener (dunno if ESC is the correct keycode, I use it for the enter key)
B4X:
Sub keyListener(e As Event)
    Dim KE As Reflector
    KE.Target = e
    Dim keycode As String
    keycode = KE.RunMethod("getCode")
    If keycode = "ESC" Then
        ...
    End If
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you check the JavaFX Docs for Button (https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html) there are two properties that allow you to set default actions when Esc and Enter are pressed on a form provided that the actions are not consumed elsewhere.

You can set them with JavaObject:

B4X:
    Dim JO As JavaObject = btnEsc
    JO.RunMethod("setCancelButton",Array(True))  'If Esc pressed
    JO = btnDef
    JO.RunMethod("setDefaultButton",Array(True))  'If Enter pressed

Simple full example attached.

Hope this helps.
 

Attachments

  • DefButtonTest.zip
    2.1 KB · Views: 400
Last edited:
Upvote 0
Top