【发布时间】:2015-08-24 17:52:37
【问题描述】:
我正在寻找一种方法来检测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 应用程序,所以我没有想出什么办法。
更新:
在寻找一种方法时,我看到一些线程告诉子类
NSView但我不知道这应该如何工作(这可能是因为我真的是编程新手,甚至不知道如何到“子类”)。我仍然认为有一些更简单的方法可以使用它,因为我知道的几乎每个statusBar应用程序都可以右键单击。
【问题讨论】:
-
NB 这个答案对我有用,虽然位掩码语法需要更新 Swift2 stackoverflow.com/questions/27781167/…
标签: macos swift cocoa nsstatusitem nsstatusbar