私はMacのステータスバーにある簡単なプログラムを開発しようとしています。私はそれが必要なので、左クリックすると機能が実行されますが、右クリックするとAboutとQuit項目のメニューが表示されます。左と右クリックステータスバー項目Mac Swift 2
私は探していましたが、私が知ることができたのはコマンドまたはコントロールのクリック提案でしたが、私はこのルートに行かない方がよいでしょう。
ご協力いただきありがとうございます。
私はMacのステータスバーにある簡単なプログラムを開発しようとしています。私はそれが必要なので、左クリックすると機能が実行されますが、右クリックするとAboutとQuit項目のメニューが表示されます。左と右クリックステータスバー項目Mac Swift 2
私は探していましたが、私が知ることができたのはコマンドまたはコントロールのクリック提案でしたが、私はこのルートに行かない方がよいでしょう。
ご協力いただきありがとうございます。
この場合、statusItemボタンのプロパティを使用できます。
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
let statusButton = statusItem!.button!
statusButton?.target = self // or wherever you implement the action method
statusButton?.action = "statusItemClicked:" // give any name you want
statusButton?.sendActionOn(Int((NSEventMask.LeftMouseUpMask | NSEventMask.RightMouseUpMask).rawValue)) // what type of action to observe
あなたは、私が "statusItemClicked"
func statusItemClicked(sender: NSStatusBarButton!){
var event:NSEvent! = NSApp.currentEvent!
if (event.type == NSEventType.RightMouseUp) {
statusItem?.menu = myMenu //set the menu
statusItem?.popUpStatusItemMenu(myMenu)// show the menu
}
else{
// call your function here
}
}
スウィフト3
let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
if let button = statusItem.button {
button.action = #selector(self.statusBarButtonClicked(sender:))
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}
func statusBarButtonClicked(sender: NSStatusBarButton) {
let event = NSApp.currentEvent!
if event.type == NSEventType.rightMouseUp {
print("Right click")
} else {
print("Left click")
}
}
スウィフト4
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem.button {
button.action = #selector(self.statusBarButtonClicked(_:))
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}
func statusBarButtonClicked(sender: NSStatusBarButton) {
let event = NSApp.currentEvent!
if event.type == NSEvent.EventType.rightMouseUp {
print("Right click")
} else {
print("Left click")
}
}
という名前を付け、上記のコードでは、アクション機能を実装します
より長い投稿https://samoylov.eu/2016/09/14/handling-left-and-right-click-at-nsstatusbar-with-swift-3/
Swift 2では、 'NSEventMask'オペランドで' | '演算子を使用するとエラーが発生します。 – beeb
statusButton?.sendActionOn(Int(NSEventMask.RightMouseUpMask.rawValue | NSEventMask.LeftMouseUpMask.rawValue))はSwift 2で動作しています – Hampus