I wanted to be able to set the keyEquivalent of an NSMenuItem based on an NSString, e.g. : CMD+R, ALT+SHIFT+D, etc
For this, I created a method in an NSString category as following (debugging NSLogs included) :
- (NSDictionary*)toKeyEquivalent
{
NSMutableDictionary* result = [NSMutableDictionary dictionary];
NSArray* parts = [self componentsSeparatedByString:@"+"];
NSLog(@"parts :%@",parts);
if ([[parts objectAtIndex:0] isEqualToString:@""])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"",@"key",[NSNumber numberWithInt:0],@"mask", nil];
}
else
{
[result setValue:[parts lastObject] forKey:@"key"];
int mask = 0;
for (NSString* p in parts)
{
if ([p isEqualToString:@"CMD"])
{
NSLog(@"cmd");
mask |= NSCommandKeyMask;
}
else if ([p isEqualToString:@"SHIFT"])
{
NSLog(@"shift");
mask |= NSShiftKeyMask;
}
else if ([p isEqualToString:@"CTRL"])
{
NSLog(@"ctrl");
mask |= NSControlKeyMask;
}
else if ([p isEqualToString:@"ALT"])
{
NSLog(@"alt");
mask = NSAlternateKeyMask;
}
}
[result setValue:[NSNumber numberWithInt:mask] forKey:@"mask"];
return result;
}
}
Now, I'm testing that using CMD+R.
The NSLog output is :
2012-04-03 10:36:19.051 App[4654:903] parts :( CMD, R )
2012-04-03 10:36:19.052 App[4654:903] cmd
2012-04-03 10:36:19.052 App[4654:903] keyEq : { key = R; mask = 1048576; } - for Run
Which looks good; mask=1048576 means 1<<20 which is equal to NSCommandKeyMask.
In other words, it SEEMS as if the Key Equivalent (Command-R) has been correctly interpreted.
The weird thing, though, is that when the menu item finally appears, the Key Equivalent appearing next to is "Command-SHIFT-R".... :-S
Why is that? Any ideas?
Did you pass in r or R? I think if you pass it an upper case string, it automatically adds the shift requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With