Swift: right / left click on NSStatusBar

I am creating an application NSStatusBarand want to call different functions depending on whether the user clicked the icon on the left or right.

Here is what I still have:

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

func applicationDidFinishLaunching(aNotification: NSNotification) {
    let icon = NSImage(named: "statusIcon")
    icon?.setTemplate(true)

    statusItem.image = icon
    statusItem.menu = statusMenu
}

In doing so, it displays with statusMenuevery click. How can I distinguish mouseEvents?

0
source share
1 answer

This snippet

func menuSelected (sender:AnyObject) {
  var clickMask: Int = 0
  if let clickEvent = NSApp.currentEvent! {  // see what caused calling of the menu action
    // modifierFlags contains a number with bits set for various modifier keys
    // ControlKeyMask is the enum for the Ctrl key
    // use logical and with the raw values to find if the bit is set in modifierFlags
    clickMask = Int(clickEvent.modifierFlags.rawValue) & Int(NSEventModifierFlags.ControlKeyMask.rawValue)
  }
  if clickMask != 0 { ... } // click with Ctrl pressed
}

from my code is checked for ctrl-click. It is placed in a method that is called from a menu item as follows:

let item = NSMenuItem(title: title, action: Selector("menuSelected:"), keyEquivalent: "")
0
source

All Articles