Sign up

Shift+F6 as menu item shortcut

In my current code I have the following:

MenuItem runFileItem = new MenuItem((MenuItem mi) => pm.runFile(), "_Run file", "activate", true, accelGroup, 'r', 
                                          ModifierType.CONTROL_MASK | ModifierType.SHIFT_MASK, AccelFlags.VISIBLE);

which works as expected - I can execute the handler pm.runFile() by pressing CTRL+SHIFT+r. Reason why I did it this way is because I could not figure out how to implement the preferred SHIFT+F6 that I wanted to use for this purpose.

So, I have two questions:

1) how to modify the above code to make SHIFT+F6 combination be handled instead?

2) Ultimately, in the distant future, I would like to make it possible for user to define this key combination, and persist it. So any guidance how to do this would be appreciated.

Re: Shift+F6 as menu item shortcut

On 03-07-2022 13:35, Dejan Lekic wrote:

In my current code I have the following:

MenuItem runFileItem = new MenuItem((MenuItem mi) => pm.runFile(), "_Run file", "activate", true, accelGroup, 'r',
                                           ModifierType.CONTROL_MASK | ModifierType.SHIFT_MASK, AccelFlags.VISIBLE);

which works as expected - I can execute the handler pm.runFile() by pressing CTRL+SHIFT+r. Reason why I did it this way is because I could not figure out how to implement the preferred SHIFT+F6 that I wanted to use for this purpose.

So, I have two questions:

1) how to modify the above code to make SHIFT+F6 combination be handled instead?

2) Ultimately, in the distant future, I would like to make it possible for user to define this key combination, and persist it. So any guidance how to do this would be appreciated.

I believe it should work if you replace 'r' with GdkKeysyms.GDK_F6.

This constructor doesn't allow changing the keys during runtime, for
that you will want to use MenuItem.setAccelPath and the related functions.

Re: Shift+F6 as menu item shortcut

On Sun, 3 Jul 2022 14:30:27 +0200, Mike Wey wrote:

I believe it should work if you replace 'r' with GdkKeysyms.GDK_F6.

This constructor doesn't allow changing the keys during runtime, for
that you will want to use MenuItem.setAccelPath and the related functions.

I should have mentioned that I have already tried that. It did not work. Maybe because accelKey is char while Gdk.Keysyms.GDK_F6 is an enum? I will give it another try.

Re: Shift+F6 as menu item shortcut

On Sun, 03 Jul 2022 16:29:36 GMT, Dejan Lekic wrote:

I should have mentioned that I have already tried that. It did not work. Maybe because accelKey is char while Gdk.Keysyms.GDK_F6 is an enum? I will give it another try.

Still no luck...

Re: Shift+F6 as menu item shortcut

With help of Adam, in this SO thread - https://stackoverflow.com/questions/72884882/shift-f6-as-menu-shortcut/7291119 - I managed to make it work as I wanted:

  MenuItem runFileItem = new MenuItem("_Run file", (MenuItem mi) => pm.runFile(), "activate");
  runFileItem.addAccelerator("activate", accelGroup, GdkKeysyms.GDK_F6, ModifierType.SHIFT_MASK, AccelFlags.VISIBLE);

It would be nice if MenuItem constructor accepted int instead of char, so that we can pass GdkKeysym constant if needed, or char, so we do not have to call addAccelerator()...