CocoaDev

Edit AllPages

This uses UndocumentedGoodness, so maybe don’t rely on it too much. It sets the dynamic menu item attribute, which allows the menu to change depending on which keys are held down. For usage, see Menus.h – DustinVoss

I think this has been properly exposed in the InterfaceBuilder now. You can set up consecutive menu items and set them as “alternates”, based on the modifier keys. This causes the NSMenuItem underneath to change - meaning you can bind separate text, icons and actions to the alternates. – MikeSolomon

Correct. The new Panther APIs are alternate and setAlternate, making these obsolete.

If you want to do this in Jaguar or below without using the undocumented parts, you can subclass NSWindow for the main window of your app, and override the - (void)flagsChanged:(NSEvent *)theEvent method. In your method you can change the NSMenuItem which is to have an option key alternative using setTitle:, setAction and setKeyEquivalent:

This won’t work if the user has already clicked on the menu though, will it? It seems to me that the application doesn’t get the flagsChanged: message when the menu is showing…


Header file for NSMenuItemCategory+CocoaDevUsersAdditions:

#import <AppKit/AppKit.h>

@interface NSMenuItem (CocoaDevUsersAdditions)

Implementation file for NSMenuItemCategory+CocoaDevUsersAdditions:

#import “NSMenuItemCategory+CocoaDevUsersAdditions.h” #import <Carbon/Carbon.h>

@implementation NSMenuItem (NSMenuItemExtension)

// Must be called after NSApp delegate’s applicationDidFinishLaunching.

@end

Category:CocoaDevUsersAdditions