我正在構建一個狀態欄應用程序(Swift 3),並且想要根據用戶點擊左側還是右側來調用不同的操作。這是我到目前爲止:Swift:識別NSStatusItem上的左鍵和右鍵事件
var statusItem = NSStatusBar.system().statusItem(withLength: -1)
statusItem.action = #selector(AppDelegate.doSomeAction(sender:))
let leftClick = NSEventMask.leftMouseDown
let rightClick = NSEventMask.rightMouseDown
statusItem.button?.sendAction(on: leftClick)
statusItem.button?.sendAction(on: rightClick)
func doSomeAction(sender: NSStatusItem) {
print("hello world")
}
我的功能沒有被調用,我找不到我們的原因。我感謝任何幫助!
這確實有效,但似乎只喜歡'.rightMouseUp',而不是'.rightMouseDown' – joe
你也改變了'button.sendAction'行嗎? –
doh!在'sendAction'中更改,不在聲明中!工作得很好,謝謝x) – joe