简体   繁体   English

在NSMenuItem上使用keyEquivalent的奇怪行为

[英]Weird behavior using keyEquivalent on NSMenuItem

I wanted to be able to set the keyEquivalent of an NSMenuItem based on an NSString , eg : CMD + R , ALT + SHIFT + D , etc 我希望能够基于NSString设置NSMenuItemkeyEquivalent ,例如: CMD + RALT + SHIFT + D

For this, I created a method in an NSString category as following (debugging NSLog s included) : 为此,我在NSString类别中创建了一个方法,如下所示(包括调试NSLog ):

- (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 . 现在,我正在测试使用CMD + R.

The NSLog output is : NSLog输出是:

2012-04-03 10:36:19.051 App[4654:903] parts :( CMD, R ) 2012-04-03 10:36:19.051 App [4654:903]部分:( CMD,R)

2012-04-03 10:36:19.052 App[4654:903] cmd 2012-04-03 10:36:19.052 App [4654:903] cmd

2012-04-03 10:36:19.052 App[4654:903] keyEq : { key = R; 2012-04-03 10:36:19.052 App [4654:903] keyEq:{key = R; mask = 1048576; mask = 1048576; } - for Run } - 用于运行

Which looks good; 哪个好看; mask=1048576 means 1<<20 which is equal to NSCommandKeyMask . mask=1048576表示1<<20 ,等于NSCommandKeyMask

In other words, it SEEMS as if the Key Equivalent ( Command - R ) has been correctly interpreted. 换句话说,它似乎已经正确解释了Key Equivalent( Command - R )。

The weird thing, though, is that when the menu item finally appears, the Key Equivalent appearing next to is " Command - SHIFT - R ".... :-S 但奇怪的是,当菜单项最终出现时,旁边出现的Key Equivalent是“ Command - SHIFT - R ”......: - S

Why is that? 这是为什么? Any ideas? 有任何想法吗?

Did you pass in r or R ? 你输入r还是R I think if you pass it an upper case string, it automatically adds the shift requirement. 我认为如果你传递一个大写字符串,它会自动添加转换要求。

Sounds like you're passing an uppercase letter to setKeyEquivalent: . 听起来你正在将一个大写字母传递给setKeyEquivalent: . The docs have this to say: 文档有这样的说法:

Discussion 讨论
This method considers the case of the letter passed to determine if it has a Shift modifier added. 此方法考虑传递的字母大小写,以确定是否添加了Shift修饰符。 That is, [item setKeyEquivalent:@"w"] sets the key equivalent to Command-w, while [item setKeyEquivalent:@"W"] is Command-Shift-w. 也就是说, [item setKeyEquivalent:@"w"]设置等效于Command-w的键,而[item setKeyEquivalent:@"W"]是Command-Shift-w。

You'll need to do a bit more processing on the entered string, and make sure that the letter is lowercase if "SHIFT" isn't present. 您需要对输入的字符串进行更多处理,如果“SHIFT”不存在,请确保该字母为小写。

You should also (eventually) consider being more liberal in your accepted input -- this code won't work if the input is "Shift" or "shift", both of which are reasonable variations. 你也应该(最终)考虑在你接受的输入中更自由 - 如果输入是“Shift”或“shift”,这个代码将不起作用,这两个都是合理的变化。

I also think that you have a bug/typo: your assignment to mask in the "Alt" key branch seems to be missing the OR: 我也认为你有一个bug /拼写错误:你在“Alt”键分支中的mask分配似乎缺少了OR:

mask = NSAlternateKeyMask;
// should be mask |= NSAlternateKeyMask;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM