backspaceKey or deleteKey for menu commands don't show


#1

I i create a KeyPress to use as a shortcut for a command that contains the deleteKey or backspaceKey they won’t show up visually on the Mac.
So if i specify the KeyPress like this:
KeyPress(backspaceKey, ModifierKeys::commandModifier, 0 )
The command get’s triggered but no visual representation will be visible in the menu

I found some info about this in the NSMenuItem reference
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenuItem_Class/Reference/Reference.html
This is the important part.
If you want to specify the Backspace key as the key equivalent for a menu item, use a single character string with NSBackspaceCharacter (defined in NSText.h as 0x08) and for the Forward Delete key, use NSDeleteCharacter (defined in NSText.h as 0x7F). Note that these are not the same characters you get from an NSEvent key-down event when pressing those keys.

So i tried this, and of course it shows in the menu but it won’t actually trigger the command.
KeyPress(0x08, ModifierKeys::commandModifier, 0 )

Any thoughts on how to fix this elegantly Jules?


#2

Did you see the stuff in juce_mac_MainMenu.mm, line 249?


#3

Yes i did actually, you basically block backspace and delete. I did mention the shortcuts are working, i am using Command+backspace not backspace only.


#4

Yeah, like the comment says, I looked at this before but couldn’t figure out a workaround for it, which is why I removed them. Sorry, haven’t got any new ideas to suggest there.


#5

I changed the code a bit so that it will check if any modifiers are set.
If not it will block backSpace and delete.
This way Command+Backspace will work but Backspace alone won’t.

                    const KeyPress& kp = keyPresses.getReference(0);
					///we ignore backspace and delete because it flashes the menu bar
					///every time you press these keys while editing text
					bool ignoreKeypress = (kp.getKeyCode() == KeyPress::backspaceKey || kp.getKeyCode() == KeyPress::deleteKey) &&
										   kp.getModifiers().isAnyModifierKeyDown();					
                    if (!ignoreKeypress)					
					{
                        juce_wchar key = kp.getTextCharacter();
                        if (key == 0)
                            key = (juce_wchar) kp.getKeyCode();

                        [item setKeyEquivalent: juceStringToNS (String::charToString (key))];
                        [item setKeyEquivalentModifierMask: juceModsToNSMods (kp.getModifiers())];
                    }

#6

Thanks - that seems like a good compromise, I’ll add something along those lines!