B4J Question About MenuItem Shortcut

Status
Not open for further replies.

Saverio

Member
Licensed User
Longtime User
Assign just "F1" key, without any Modifier, to a MenuItem, it will not fire any event.
Unless handle by code the KeyEvent.
Even setting all the modifiers to "Ignore"(ref. KeyCombination: getName) that will set all the modifier state as "ANY".
At least on my pc, win7 - jdk1.8.0_131 - B4J v5.51, with Italian keyboard layout.
As I read on the net, it is an intetional limitation by javafx team.
So I say, why show "F1" as shortcut, in the menuitem, and then do not fire any event?

Note: I'm not saying that it is a B4J fault.

Maked just fresh app with a MenuBar and a TextArea. (see attachment "Shortcut1.zip")
When the TextArea has the focus, here, isn't work. (in the case TextArea has ever the focus)
A Shortcut should behave system wide or, at least, application wide.

As a workaround I have catched all events in the main stage and then redirect them
to the MenuBar only when is just F1 key interested and none of the modifiers.
This workaround is just a starting point. It work just for F1 key but can be implemented for
anything else. And, of course, it can be done in other several ways. For example it could be done
via java inline...and so on. (see attachment "Shortcut2.zip")

Any idea?
 

Attachments

  • Shortcut1.zip
    2.2 KB · Views: 347
  • Shortcut2.zip
    2.7 KB · Views: 343

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is a bug in JavaFX: https://bugs.openjdk.java.net/browse/JDK-8148857 (fixed for Java 9)

My solution with event filter:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
   MainForm.Show
   Dim r As Reflector
   r.Target = MainForm.RootPane
   r.AddEventFilter("Key", "javafx.scene.input.KeyEvent.KEY_PRESSED")
End Sub

Sub Key_Filter (EventData As Event)
   Dim jo As JavaObject = EventData
   Dim code As String = jo.RunMethod("getCode", Null)
   If code = "F1" Then
     mnuHelp_Action
     EventData.Consume
   End If
End Sub

Sub mnuHelp_Action
   
End Sub
 
Upvote 0

Saverio

Member
Licensed User
Longtime User
My solution with event filter:
Your solution isn't work properly :)
It will fire events even if press, all or some, modifier(s).
Mine, control all the modifiers state before redirect the event.
By the way, it was just a clue.
What's important is that we know the issue and, whether needed, we have some workaround.
 
Upvote 0

PatriX

New Member
You can add for CTRL, SHIFT, ALT:
B4X:
Sub Key_Filter (EventData As Event)
   Dim jo As JavaObject = EventData
   Dim code As String = jo.RunMethod("getCode", Null)
   'add this
    Dim isAltDown As Boolean = jo.RunMethod("isAltDown", Null)
    Dim isShiftDown As Boolean = jo.RunMethod("isShiftDown", Null)
    Dim isControlDown As Boolean = jo.RunMethod("isControlDown", Null)

   If code = "F1" Then
     mnuHelp_Action
    'and this
   Else If code = "V" And isControlDown And isShiftDown Then
        LogFormat("V And isControlDown And isShiftDown",code)
        YourSubCTRL_SHIFT_V
   End If
    EventData.Consume
End Sub

Sub mnuHelp_Action
  
End Sub
'and this
Sub YourSubCTRL_SHIFT_V
  
End Sub
 
Upvote 0
Status
Not open for further replies.
Top