Search code examples
cocoamacosmenunsmenuitemglyph

Programmatically adding glyphs (delete key, backspace, space, etc) to menu item


It used to be that in Carbon you could use SetMenuItemKeyGlyph. What's the alternative under 10.6? Will I need to use undocumented goodness or...?

Thanks


Solution

  • Use -[NSMenuItem setKeyEquivalent:] and give it an NSString of the character you want to be used. NSMenuItem will handle translating @" " into Space for you, etc.

    Delete key (aka "Backspace". This is the regular delete button on your keyboard):

    [myMenuItem setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x08]];
    

    Forward delete key (The "del" key):

    [myMenuItem setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x7f]];
    

    Space:

    [myMenuItem setKeyEquivalent:@" "];
    

    Tab:

    [myMenuItem setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x09]];