On May 19, 2014, at 1:30 PM, Seth Willits <sli...@araelium.com> wrote:

> Any ideas on how to get a Cancel button which is both the default button and 
> responds to escape? Both require setting the button's key equivalent and 
> there can only be one. 


Desired Behavior:

        Delete - First button, but *not* default button. Responds to 
Command-Delete
        Cancel - Default button, responds to both the Return and Escape keys.
        

Solution:

        NSAlert * alert = [[NSAlert alloc] init];
        [alert addButtonWithTitle:@"Delete"];
        [alert addButtonWithTitle:@"Cancel"];
        [[alert.buttons objectAtIndex:0] 
setKeyEquivalentModifierMask:NSCommandKeyMask];
        [[alert.buttons objectAtIndex:0] setKeyEquivalent:@"\b"]; // Cmd-Delete
        [[alert.buttons objectAtIndex:1] setKeyEquivalent:@"\r"]; // Return
        
        NSButton * otherButton = [[NSButton alloc] initWithFrame:NSMakeRect(0, 
0, 0, 0)];
        otherButton.target = alert.buttons.lastObject;
        otherButton.action = @selector(performClick:);
        otherButton.keyEquivalent = @"\e"; // Escape
        [alert setAccessoryView:otherButton];



--
Seth Willits




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to