NSClickGestureRecognizer 不适用于 NSStatusItem

NSClickGestureRecognizer not working on NSStatusItem

试图识别右键单击 NSStatusItem 我得到了一个建议 ( 谢谢 to Zoff Dino ) 为此使用 NSClickGestureRecognizer。但是由于某些奇怪的原因,它没有按应有的方式工作。我能够识别左键单击 (buttonMask = 0x1) 但不能识别右键单击 (buttonMask = 0x2)。这就是我希望它的工作方式,但事实并非如此:

func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application


    if let button = statusItem.button {

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


    }}

func rightClickAction(sender: NSGestureRecognizer) {
    if let button = sender.view as? NSButton {
        NSLog("rightClick")
    }
}

UPDATE:

I still did not manage to gets to work. Somehow it doesn't react on a right click (but changing the code on a left click) does. I guess some really simple issues are occurring that seem to block it from working. Even stranger is the fact that gesture.buttonMask = 0x1 works on the left click.

替代 NSClickGestureRecognizer 的解决方案是将自定义视图附加到状态栏并从那里处理事件。

小缺点是您必须处理绘图和菜单委托方法。

这里有一个简单的例子:

创建文件 StatusItemView NSView

的子类
import Cocoa

class StatusItemView: NSView, NSMenuDelegate {

  //MARK: - Variables

  weak var statusItem : NSStatusItem!
  var menuVisible = false

  var image : NSImage! {
    didSet {
      if image != nil {
        statusItem.length = image.size.width
        needsDisplay = true
      }
    }
  }

  //MARK: - Override functions

  override func mouseDown(theEvent: NSEvent) {
    if let hasMenu = menu {
      hasMenu.delegate = self
      statusItem.popUpStatusItemMenu(hasMenu)
      needsDisplay = true
    }
  }

  override func rightMouseDown(theEvent: NSEvent) {
    Swift.print(theEvent)
  }

  //MARK: - NSMenuDelegate

  func menuWillOpen(menu: NSMenu) {
    menuVisible = true
    needsDisplay = true
  }

  func menuDidClose(menu: NSMenu) {
    menuVisible = false
    menu.delegate = nil
    needsDisplay = true
  }

  //MARK: - DrawRect

  override func drawRect(dirtyRect: NSRect) {
    statusItem.drawStatusBarBackgroundInRect(bounds, withHighlight:menuVisible)
    let origin = NSMakePoint(2.0, 3.0) // adjust origin if necessary
    image?.drawAtPoint(origin, fromRect: dirtyRect, operation: .CompositeSourceOver, fraction: 1.0)
  }
}

AppDelegate 中,您需要对自定义菜单的引用和 NSStatusItem 实例

的实例变量
 @IBOutlet weak var menu : NSMenu!
 var statusItem : NSStatusItem!

applicationDidFinishLaunching 中创建视图并将其附加到状态项。请注意在附加视图后设置视图的图像,以确保考虑到宽度。

  func applicationDidFinishLaunching(aNotification: NSNotification) {
    statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1) // NSVariableStatusItemLength)
    let statusItemView = StatusItemView(frame: NSRect(x: 0.0, y: 0.0, width: statusItem.length, height: 22.0))
    statusItemView.statusItem = statusItem;
    statusItemView.menu = menu
    statusItem.view = statusItemView
    statusItemView.image = NSImage(named: NSImageNameStatusAvailable)
  }

未实现控制点击触发右键功能的特例