Re: NSButton "reveals" keyboard shortuct [SOLVED]
Re: NSButton "reveals" keyboard shortuct [SOLVED]
- Subject: Re: NSButton "reveals" keyboard shortuct [SOLVED]
- From: Pierre Bernard <email@hidden>
- Date: Wed, 23 Apr 2008 20:48:03 +0200
Answering part 1 of my own question: revealing the keyboard shortcut
once the command key is pressed. C.f. below for the code
What remains is the second part: how does NSButton implement keyboard
shortcuts. How would I implement and additional shortcut for a button
which already has one.
Best,
Pierre Bernard
Houdah Software s.à r.l.
extern NSUInteger carbonToCocoaModifierFlags(UInt32
carbonModifierFlags);
extern NSString *stringForCocoaModifierFlagsAndKeyCode(NSUInteger
flags, NSString *keyEquivalent);
@implementation HHShortcutButton
@synthesize baseTitle;
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]) != nil) {
[self setBaseTitle:[self title]];
}
return self;
}
- (void)viewDidMoveToWindow
{
[super viewDidMoveToWindow];
NSWindow *window = [self window];
if (window != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidUpdate:)
name:NSWindowDidUpdateNotification object:window];
}
}
- (void)setTitle:(NSString*)aTitle
{
[super setTitle:aTitle];
[self setBaseTitle:[self title]];
}
- (void)windowDidUpdate:(NSNotification*)notification
{
if (GetCurrentEventKeyModifiers() & cmdKey) {
NSString *keyEquivalent = [self keyEquivalent];
NSUInteger keyEquivalentModifierMask = [self
keyEquivalentModifierMask];
[super
setTitle:stringForCocoaModifierFlagsAndKeyCode
(keyEquivalentModifierMask, keyEquivalent)];
}
else {
[super setTitle:[self baseTitle]];
}
}
@end
extern NSUInteger carbonToCocoaModifierFlags(UInt32 carbonModifierFlags)
{
NSUInteger cocoaModifierFlags = 0;
if (carbonModifierFlags & shiftKey) {
cocoaModifierFlags |= NSShiftKeyMask;
}
if (carbonModifierFlags & cmdKey) {
cocoaModifierFlags |= NSCommandKeyMask;
}
if (carbonModifierFlags & optionKey) {
cocoaModifierFlags |= NSAlternateKeyMask;
}
if (carbonModifierFlags & controlKey) {
cocoaModifierFlags |= NSControlKeyMask;
}
return cocoaModifierFlags;
}
extern NSString *stringForCocoaModifierFlagsAndKeyCode(NSUInteger
flags, NSString *keyEquivalent)
{
return [NSString stringWithFormat:@"%@%@%@%@%@",
(flags & NSCommandKeyMask ? [NSString stringWithFormat:@"%C + ",
kCommandUnicode] : @""),
(flags & NSAlternateKeyMask ? [NSString stringWithFormat:@"%C + ",
kOptionUnicode] : @""),
(flags & NSControlKeyMask ? [NSString stringWithFormat:@"%C + ",
kControlUnicode] : @""),
(flags & NSShiftKeyMask ? [NSString stringWithFormat:@"%C + ",
kShiftUnicode] : @""),
[keyEquivalent uppercaseString]];
}
On 22 Apr 2008, at 17:48, Pierre Bernard wrote:
Hi!
I love how the buttons in some "save before closing" sheets reveal
themselves by being added to the button name when the command key is
held for a while.
The one example I can think of is AppleWorks 6. Wish all apps /
buttons had that.
Is this a Carbon thing?
How would I go about to implement this?
Seeing that the button in question is not in the responder chain, I
guess the implementation ought to be moved to the window or window
controller. This doesn't strike me as the best place. I'd rather
have a reusable NSButton subclass.
Such buttons also may have multiple shortcuts. E.g. the default save
button is triggered by both Enter and cmd-S. This does not seem to
be possible with NSButton. Again I wonder if I should override
keyDown: in my window controller.
BTW, how are keyboard shortcuts on NSButtons implemented? Do they
somehow register with the window for events or does the window walk
the subcomponents to find one which may respond?
Best,
Pierre
---
Pierre Bernard
http://www.bernard-web.com/pierre
http://www.houdah.com
_______________________________________________
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
---
Pierre Bernard
http://www.bernard-web.com/pierre
http://www.houdah.com
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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