AppDelegate.swift
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
var button: NSStatusBarButton!
func applicationDidFinishLaunching(_ aNotification: Notification) {
button = statusItem.button!
button.title = "🤔"
button.action = #selector(clicked(_:))
// ここで受け取るイベントを登録しておく
button.sendAction(on: [.leftMouseDown, .rightMouseDown])
}
@objc func clicked(_ sender: NSStatusBarButton) {
let event = NSApp.currentEvent!
switch event.type {
case .rightMouseUp:
print("right click")
case .leftMouseUp:
print("left click")
default:
break
}
}
}