B4J Question B4J keyboard shortcuts: how to combine KeyPressedListener and Menu ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Happy NY 2025 !

When i use CTRL and SHIFT with KeyPressedListener - shortcuts of the Menu (Ctrl+O, Ctrl+N - "javafx.scene.input.KeyCombination") stopped to work (initially were OK).
KeyPressedListener is used on several B4XPages, i have to syncronize the states from other pages to Main:
B4X:
Private Sub KeyPressed_Event(e As Event)
    Dim KE As Reflector
    KE.Target = e 'e is a KeyEvent instance
    Dim KeyCode As String = KE.RunMethod("getCode")
    Dim EventType As String = KE.RunMethod("getEventType")
 
 
    Select EventType
   ...
    End Select
    Main.KEY_CTRL_PRESSED = KEY_CTRL_PRESSED
    Main.KEY_SHIFT_PRESSED = KEY_SHIFT_PRESSED
    e.Consume
End Sub


Possible to combine working Menu and pages?
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Thanks, Aeric, but only this one of 2018 is similar, where Erel offered just workaround (Menu's calls are processed in KeyPressedListener).
Maybe any other way in 2025 and Java 19 ?

So, it needs to combine working of this code and KeyPressedListener:

B4X:
    'Set a shortcut key for this menu item
    'Returns the menu item
Public Sub SetShortCutKey(MI As JavaObject,Combination() As String) As MenuItem
    Dim KC As JavaObject
    KC.InitializeStatic("javafx.scene.input.KeyCombination")
    Dim KCS As String
    For i = 0 To Combination.Length - 1
        If i > 0 Then KCS = KCS & "+"
        KCS = KCS & Combination(i)
    Next
    MI.RunMethod("setAccelerator",Array(KC.RunMethod("keyCombination",Array(KCS))))
    Return MI
End Sub

Or is it impossible ?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thanks, Aeric, but only this one of 2018 is similar, where Erel offered just workaround (Menu's calls are processed in KeyPressedListener).
Maybe any other way in 2025 and Java 19 ?

So, it needs to combine working of this code and KeyPressedListener:

B4X:
    'Set a shortcut key for this menu item
    'Returns the menu item
Public Sub SetShortCutKey(MI As JavaObject,Combination() As String) As MenuItem
    Dim KC As JavaObject
    KC.InitializeStatic("javafx.scene.input.KeyCombination")
    Dim KCS As String
    For i = 0 To Combination.Length - 1
        If i > 0 Then KCS = KCS & "+"
        KCS = KCS & Combination(i)
    Next
    MI.RunMethod("setAccelerator",Array(KC.RunMethod("keyCombination",Array(KCS))))
    Return MI
End Sub

Or is it impossible ?
You mean the code works in Java below 19 and not in Java 19?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
You mean the code works in Java below 19 and not in Java 19?
No, your found post was about old Java version and workaround.
Now all is more modern, but how to combine Menu and KeyboardListener ?
 
Last edited:
Upvote 0
Top