迅捷:右/左单击NSStatusBar [英] Swift: Right/Left click on NSStatusBar

查看:119
本文介绍了迅捷:右/左单击NSStatusBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个NSStatusBar应用,并希望根据用户是在图标上单击左还是右来调用不同的功能.

I'm building a NSStatusBar app and want to call different functions depending on if the user clicked left- or right on the icon.

这是我到目前为止所拥有的:

Here is what I have so far:

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.如何区分mouseEvent?

With this it shows up the statusMenu by every click. How can I distinguish the mouseEvents?

推荐答案

此代码段

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单击.它位于从这样的菜单项调用的方法中:

from my code checks for a ctrl-click. It's place in the method that is called from a menu item like this:

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

这篇关于迅捷:右/左单击NSStatusBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆