10

我是一名 .Net 开发人员,需要将一个小项目移植到 Mac 中,所以我对 Objective C 几乎一无所知。事实上,下面的代码只是一堆抓着稻草在黑暗中拍摄的东西。

尝试构建一个状态菜单程序,根据它是左键单击还是右键单击/ctrl+单击来打开一个或另一个窗口。这是我所拥有的,它仅适用于左键单击(显然):

-(void) awakeFromNib{

    NSBundle *bundle = [NSbundle mainBundle];

    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
    [statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
    [statusItem setImage:statusImage];
    [statusItem setToolTip:@"Program Name"];
    [statusItem setHighlightMode:YES];
    [statusItem setAction:@selector(openWin:)];
    [statusItem setTarget: self];
}

-(void)openWin:(id)sender{
    [self openLeftWindow:sender];
}

-(IBAction)openLeftWindow:(id)sender{
    //Code to populate Left Click Window
    [leftWindow makeKeyAndorderFront:nil];
}

-(IBAction)openRightWindow:(id)sender{
    //Code to populate Right Click Window
    [rightWindow makeKeyAndorderFront:nil];
}

在我看来,解决方案可能是 openWin() 函数中的 if 语句来确定单击了哪个按钮(或者是否按住 ctrl)然后运行适当的代码或使菜单同时知道左右点击。但是当我尝试这样做时,这些都没有奏效。

提前致谢。

4

3 回答 3

12

如果您对检测到控制单击而不是右键单击感到满意,那么第一个代码块将执行您想要的操作。如果您确实需要右键单击检测,则必须在 NSStatusItem 中使用自定义视图而不是图像,并且第二个代码块将起作用。

简单方法:

- (void)openWin:(id)sender {
    NSEvent *event = [NSApp currentEvent];
    if([event modifierFlags] & NSControlKeyMask) {
        [self openRightWindow:nil];
    } else {
        [self openLeftWindow:nil];
    }
}

自定义视图方法:

- (void)awakeFromNib {
    ...
    statusImage = ...
    MyView *view = [MyView new];
    view.image = statusImage;
    [statusItem setView:view];
    [statusItem setToolTip:@"Program Name"];
    view target = self;
    view action = @selector(openLeftWindow:);
    view rightAction = @selector(openRightWindow:);
    [view release];
    //[statusImage release]; //If you are not using it anymore, you should release it.
}

MyView.h

#import <Cocoa/Cocoa.h>
@interface MyView : NSControl {
    NSImage *image;
    id target;
    SEL action, rightAction;
}
@property (retain) NSImage *image;
@property (assign) id target;
@property (assign) SEL action, rightAction;
@end

MyView.m

#import "MyView.h"
@implementation MyView
@synthesize image, target, action, rightAction;
- (void)mouseUp:(NSEvent *)event {
    if([event modifierFlags] & NSControlKeyMask) {
        [NSApp sendAction:self.rightAction to:self.target from:self];
    } else {
        [NSApp sendAction:self.action to:self.target from:self];
    }
}
- (void)rightMouseUp:(NSEvent *)event {
    [NSApp sendAction:self.rightAction to:self.target from:self];
}
- (void)dealloc {
    self.image = nil;
    [super dealloc];
}
- (void)drawRect:(NSRect)rect {
    [self.image drawInRect:self.bounds fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
}
@end
于 2010-12-30T20:33:20.183 回答
3

我会创建一个视图并使用状态项方法。

-setView:

然后在子类视图中,您可以使用以下命令检测 ctrl+LMB

- (void)mouseDown:(NSEvent *)theEvent
{
    [super mouseDown:theEvent];

    //Respond to the mouse click
    if ([theEvent modifierFlags] & NSCommandKeyMask) //Command + LMB
    {       
      //Do something
    }
}

我想你可以弄清楚其余的。

于 2010-12-30T20:05:48.180 回答
1

更简化的响应(注意,仅适用于 control + click)

特性:

@property (strong, nonatomic) NSStatusItem *statusItem;
@property (weak) IBOutlet NSMenu *statusMenu;

在您的应用程序中加载:

[self.statusItem setAction:@selector(itemClicked:)];

点击功能:

- (void)itemClicked:(id)sender
{
    NSEvent *event = [NSApp currentEvent];

    if([event modifierFlags] & NSControlKeyMask) {
        NSLog(@"Right Click Pressed");
        [self.statusItem popUpStatusItemMenu:self.statusMenu];

    } else {
        // Do Nothing
        NSLog(@"Left Click Pressed");
    }
}
于 2015-08-16T02:01:46.103 回答