右键单击 NSStatusBarButton 时调用 Action

Call Action when NSStatusBarButton is right-clicked

我正在寻找一种方法来检测何时右键单击 NSStatusBarButton(使用 Swift)并调用操作。

我目前是这样设置的:

let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)

func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application
    if let button = statusItem.button {
        button.image = NSImage(named: "myImage")
        button.alternateImage = NSImage(named: "myImage")
        button.action = Selector("myAction")
    }
}

我想使用button.rightMouseDown(<#theEvent: NSEvent#>)(因为没有这样的"alternateAction")但不幸的是我没有想出一些东西,因为我刚开始编程Mac 应用程序。

Update:

While searching for a way to do this I saw some threads telling to subclass a NSView but I don't se how this should work (This could be because I am really new to programming and don't even know how to "subclass"). Still I thought there was some easier way to use this since nearly every statusBar App that I know rects on right-clicks.

您可以子类化并覆盖 mouseDown 方法,但是自从 Mac OS X 10.10 (Yosemite) 以来,有一种更简单的方法:NSGestureRecognizer及其子类:

func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application
    if let button = statusItem.button {
        button.image = NSImage(named: "myImage")
        button.alternateImage = NSImage(named: "myImage")
        button.action = Selector("myAction")

        // Add right click functionality
        let gesture = NSClickGestureRecognizer()
        gesture.buttonMask = 0x2 // right mouse
        gesture.target = self
        gesture.action = "myRightClickAction:"
        button.addGestureRecognizer(gesture)
    }
}

func myRightClickAction(sender: NSGestureRecognizer) {
    if let button = sender.view as? NSButton {
        // Handle your right click event here
    }
}

对于已接受答案的方法,我遇到了与您相同的问题:它不适用于 buttonMask 0x2,仅适用于 buttonMask 0x1。常规 NSButtons(但不是 NSStatusBarButtons)可以处理 NSClickGestureRecognizers,所以也许这就是回答者的想法。我发现建议的另一个解决方案是将 NSStatusItemview 设置为您自己的 NSView 的自定义子 class 的实例,但从 OS X 开始v10.10,获取或设置 view 已弃用,所以我不想那样做。

我通过添加 NSView 的自定义子 class 作为 NSStatusItem 按钮的子视图解决了这个问题。我的 NSView 实现 -rightMouseUp: 来接收鼠标右键事件,然后将该事件传递给我的 class 给它的块,它想要处理鼠标右键单击事件。

这是我的自定义子class:

#import <Cocoa/Cocoa.h>

@interface TTRightClickDetector : NSView

@property (copy) void (^onRightMouseClicked)(NSEvent *);

@end

#import "TTRightClickDetector.h"

和实施:

@implementation TTRightClickDetector

- (void)rightMouseUp:(NSEvent *)theEvent
{
    if(self.onRightMouseClicked)
    {
        self.onRightMouseClicked(theEvent);
    }
}

@end

下面是我的使用方法:

self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
NSStatusBarButton *button = self.statusItem.button;
button.image = [NSImage imageNamed:@"image"];
button.action = @selector(leftMouseClicked:);

TTRightClickDetector *rightClickDetector = [[TTRightClickDetector alloc] initWithFrame:button.frame];
rightClickDetector.onRightMouseClicked = ^(NSEvent *event){
    [self rightMouseClicked];
};
[button addSubview:rightClickDetector];

commanda 答案的 swift 版本(继承 NSView 并实现 mouseDown)。

NSView 子类:

class RightMouseHandlerView: NSView {

    var onRightMouseDown: (()->())? = nil

    override func rightMouseDown(with event: NSEvent) {
        super.rightMouseDown(with: event)

        if onRightMouseDown != nil {
            onRightMouseDown!()
        }
    }
}

然后添加到状态栏按钮并设置代码块:

statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)

if let button = statusItem.button {
    let rmhView = RightMouseHandlerView(frame: statusItem.button!.frame)
    rmhView.rightMouseDown = {
        // Do something when right mouse down on button
    }
    button.addSubview(rmView)
}