B4J Code Snippet B4XDialog - Add keyboard handling

Discussion in 'B4J Code Snippets' started by Erel, Apr 7, 2019.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This code allows the user to close the dialog with 'escape' or one of the following keys: Y, N or C.
    It depends on agraham's jReflection library.

    Code:
    Sub AddKeyPressedListener
       
    Dim r As Reflector
       r.Target = MainForm.RootPane
       r.AddEventHandler(
    "keypressed""javafx.scene.input.KeyEvent.KEY_PRESSED")
    End Sub

    Sub KeyPressed_Event (e As Event)
       
    If Dialog.Visible Then
           
    Dim jo As JavaObject = e
           
    Dim keycode As String = jo.RunMethod("getCode"Null)
           
    If keycode = "ESCAPE" or keycode = "C" Then
               Dialog.Close(xui.DialogResponse_Cancel)
           
    Else If keycode = "Y" Then
               Dialog.Close(xui.DialogResponse_Positive)
           
    Else If keycode = "N" Then
               Dialog.Close(xui.DialogResponse_Negative)
           
    End If
       
    End If
    End Sub
    You need to call AddKeyPressedListener once when the program starts.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice