状态项右键单击不会触发可可吗?

时间:2018-08-17 14:46:39

标签: objective-c macos cocoa nsstatusitem

我想触发statusitem的右键单击,但我能够触发左键单击,但不能触发右键单击。

-(void)applicationWillFinishLaunching:(NSNotification *)notification{

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setTitle:@"Coverdesk"];
[statusItem setEnabled:YES];

[statusItem setAction:@selector(handleStatusItemActions:)];
[statusItem setTarget:self];
[[MenuHandler defaultHandler].menu setDelegate:self];
[MenuHandler defaultHandler].delegate = self;
[statusItem setHighlightMode:YES];}


-(void)handleStatusItemActions:(id)sender{
if ([[NSUserDefaults standardUserDefaults] boolForKey:kUDKeyMouseClickPrefrence]) {

    NSEvent *event = [NSApp currentEvent];
    if(event.type == NSEventTypeLeftMouseUp) {
        NSLog(@"left");
        [self showWindow];
    }
    else if(event.type == NSEventTypeRightMouseUp) {
        NSLog(@"right");
        [self showMenu];
    }
}else{
    NSLog(@"main menu");

    [self showMenu];
}  }

上面使用的代码。

有什么建议吗?

预先感谢!

1 个答案:

答案 0 :(得分:0)

添加了这一行,它工作正常。

    [statusItem.button sendActionOn:NSEventMaskLeftMouseUp|NSEventMaskRightMouseUp];