Re: NSAlert - Default Cancel also respond to Escape? [Solution]
Re: NSAlert - Default Cancel also respond to Escape? [Solution]
- Subject: Re: NSAlert - Default Cancel also respond to Escape? [Solution]
- From: Seth Willits <email@hidden>
- Date: Sun, 08 Jun 2014 20:12:28 -0700
On May 19, 2014, at 1:30 PM, Seth Willits <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden