我正在构建NSStatusBar
应用,并希望根据用户是否在图标上向左或向右单击来调用不同的功能。
这是我到目前为止所做的:
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
let icon = NSImage(named: "statusIcon")
icon?.setTemplate(true)
statusItem.image = icon
statusItem.menu = statusMenu
}
这样每次点击都会显示statusMenu
。我如何区分mouseEvents?
答案 0 :(得分:0)
此代码段
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
}
从我的代码检查ctrl-click。它位于从菜单项调用的方法中:
let item = NSMenuItem(title: title, action: Selector("menuSelected:"), keyEquivalent: "")