is there a way to execute keyboard equivalents in a contextual menu ?

almost everything is in the title

I made a small text editor, and a contextual menu to put the selected text in bold or italic
this works, but not if I put a keyboard equivalent to my contextual menu it is not fired.
if it is in the main menubar, it gets fired.

is there a way to handle keyboard shortcuts in contextual menus ?

thanks.

If you have it in the Menubar (with Shortcut) you can clone it to the ContextMenu.

say you have a menu EditBold

base.Append new MenuItem(EditBold.Clone) Return True

I dont want to have the menu in the menubar, but only in the contextmenu.
I tried to put it in the menubar only to test the shortcut was working.
is it only possible ?

mymenuitem is a subclass of menuitem
m is a property (menuitem)

(tested in Linux)

Action Code goes to mymenuitem.action

  m =  new mymenuitem
  m.Text = "make bold"
  m.KeyboardShortcut = "Ctrl-B"
  m.AutoEnable = True
  base.Append (m)
  Return True

I often create contextual menus on the fly, it perfectly supports keyboard shortcuts :

Here, in a button

[code]Sub Action() Handles Action
dim base as new Menuitem

base.append(New MenuItem(“Import”))
dim e as New MenuItem(“Export”)
e.KeyboardShortcut = “Cmd-E”
base.append(e)
base.append(New MenuItem(MenuItem.TextSeparator))

base.append(New MenuItem(“Cut”))
base.append(New MenuItem(“Copy”))
base.append(New MenuItem(“Paste”))

Dim m as MenuItem
m=base.Popup

msgbox m.Text
End Sub
[/code]

Cmd-E appears in the Contextual menu, next to “Export”

I do something like

dim m as MenuItem m = new MenuItem(kPlain) m.KeyboardShortcut = "cmd-shift-P" base.Append m m = new MenuItem(kBold) m.KeyboardShortcut = "cmd-B" m.Name = "TextBold" base.Append m m = new MenuItem(kItalic) m.KeyboardShortcut = "cmd-I" base.Append m m = new MenuItem(kUnderlined) m.KeyboardShortcut = "cmd-U" base.Append m base.Append new MenuItem("-") return True

in the constructcontextualmenu event of a text area
and when I type cmd-B I get a beep and no bold text …
what can it be ?

try this in KeyDown

if Keyboard.ControlKey and key = "b" then if me.SelBold = True then me.SelBold = False elseif me.SelBold = False Then me.SelBold = True end if Return True end if

[quote=292300:@Jean-Yves Pochez]in the constructcontextualmenu event of a text area
and when I type cmd-B I get a beep and no bold text …
what can it be ?[/quote]

I just tried your code, and it shows all the shortcuts.

In ContextualMenuAction, I have simply placed :

msgbox hitItem.text

What is your code in there ?

yes it shows the shortcuts in the menu, but they are not working.

Have you tried adding an item with the menu bar designer, setting visible to false and then adding each item as a child of that invisible menu item?

[quote=292272:@Jean-Yves Pochez]almost everything is in the title

I made a small text editor, and a contextual menu to put the selected text in bold or italic
this works, but not if I put a keyboard equivalent to my contextual menu it is not fired.
if it is in the main menubar, it gets fired.

is there a way to handle keyboard shortcuts in contextual menus ?

thanks.[/quote]

Is this for OS X or Windows or Linux ?
They behave very differently when it comes to contextual menus

On OS X you basically can’t use shortcuts in contextual menus - at least not by just assigning them as you’ve done
You have to custom handle the keydowns etc (basically hack it as its not std behaviour)
Windows allows it as does Linux

Do you think this class might help? There may be some bug fixes you need to apply or changes to make it suite what your trying to do. Also there is no documentation but it should hopefully be pretty self explanatory. You create a new instance, pass key code as parameter and use TestPress. That should hopefully have you covered for ‘Ctrl’ + [your keycode] presses.

You could use a timer that repeats and makes this check.

It may not be a perfect emulation of native key presses either but if anybody has any improvement they have made (if they find it useful), would be nice if they could upload an improved version here.

https://www.dropbox.com/s/ah3wyhfmbsi9aei/ShortcutKey.xojo_code?dl=0

that works, you can clone the invisible MenuItem in ConstructContextualMenu and make it visible in TextArea only.

dim b as new MenuItem(EditBold.Clone) b.Visible = true base.Append b Return True

MenuHandler (EditBold)

if TextArea1.SelBold = true then TextArea1.SelBold = false else TextArea1.SelBold = true end if Return True

The reason being, that they are not necessary, as every command must appear in the main menu (even if disabled most of the time). macOS users are expecting that they can go trough the main menu and find every command. Also the shortcuts should not appear in the contextual menu, but only in the main menu.

I am on a mac and wanted an explaination on why it does not work.
thanks Norman & Eli.
I did not want to add the items in the menubar, but if it is apple rules, then I have to !
the invisible idea is nice too.
thanks to everyone.

On the bottom of macOS Human Interface Guidelines – Contextual Menus

An additional reason is: a user can change the keyboard shortcut of any menu item of an application in the system preferences. I think this would not work with keyboard shortcuts in contextual menus.

Once again, I will have to post a working project, right ?

[quote=292334:@Jean-Yves Pochez]I am on a mac and wanted an explaination on why it does not work.
thanks Norman & Eli.
I did not want to add the items in the menubar, but if it is apple rules, then I have to !
the invisible idea is nice too.
thanks to everyone.[/quote]

BS if you tell me. Absolutely no need for putting anything in the menu bar.

See this project. It works, I tested (instead of making dogmatic statements) :
https://dl.dropboxusercontent.com/u/17407375/contextualmenushortcuts.xojo_binary_project

Cmd-E does trigger the menu option just fine.

Sigh. This mania of making things complex …

[quote=292341:@Eli Ott]On the bottom of macOS Human Interface Guidelines – Contextual Menus

An additional reason is: a user can change the keyboard shortcut of any menu item of an application in the system preferences. I think this would not work with keyboard shortcuts in contextual menus.[/quote]

That would be the only reason for putting that in the bar.

Take the Xojo IDE. The bottom pane cannot be manipulated by keyboard shortcuts in a way that you can do it without mouse clicks (and as a developer I’m used to almost never use the mouse in Xcode and the like). If the menu items would be:
– Show Search Pane
– Show Error Pane
– Show Messages Pane
… I could assign in the system preferences Ctrl-1, Ctrl-2, Ctrl-3 to them (as an example).

Then there is no way as far as I know to select the navigator by a keyboard shortcut. Maybe there is even one, but since I couldn’t find it in the main menu, I’m lost and I have to use the mouse.

And there is more in the IDE which cannot be fitted to my needs by assigning keyboard shortcuts.

Eli, while your point may be valid for yourself, you are derailing the topic of the thread.

Jean-Yves simply wanted to have keyboard shortcuts in a contextual menu. That is exactly what I built a demo project for. No more.

It was no question of the IDE. That is an interesting topic, but you should start a new thread.