【问题标题】:Left vs Right Click Status Bar Item Mac Swift 2左键与右键单击状态栏项目 Mac Swift 2
【发布时间】:2016-01-20 08:43:53
【问题描述】:

我一直在尝试开发一个位于 Mac 状态栏中的简单程序。我需要它,以便如果您左键单击,它会运行一个功能,但如果您右键单击它会显示一个带有 About 和 Quit 项目的菜单。

我一直在寻找,但我只能找到命令或控制点击建议,但我不想走这条路。

在此先感谢您的帮助!

【问题讨论】:

    标签: macos menu swift2 nsstatusitem


    【解决方案1】:

    斯威夫特 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/ 提供更长的帖子

    【讨论】:

    • .popUpMenu() 自 macOS 10.14 起已弃用
    • 另外,请记住,在官方文档中,NSStatusItem 的menu 属性表示“当非零时,不使用状态项的单击操作行为。设置此值属性为 nil 删除菜单。”这意味着如果您的菜单不是 nil,那么 action 将不起作用。
    • 看来您没有设置目标:button.target = self
    【解决方案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
    

    然后你实现action函数,在上面的代码中我将它命名为“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
        }
    }
    

    【讨论】:

    • 在 Swift 2 中,在 NSEventMask 操作数上使用 | 运算符会出错。
    • statusButton?.sendActionOn(Int(NSEventMask.RightMouseUpMask.rawValue | NSEventMask.LeftMouseUpMask.rawValue)) 正在使用 Swift 2
    【解决方案3】:

    在 Swift 5 中,

    我就是这样工作的。 通过参考上述答案,我能够检测到是右键单击还是左键单击。

    但是,一旦你右键单击,你只能一直显示 NSMenu。

    然后,我添加statusItem.button?.performClick(nil) & statusItem?.menu = nil

    您可以添加statusItem?.popUpMenu(menu),但它在 macOS 10.14 中已被贬值。 (但效果很好。)

    最后,如果左键单击,它会运行一个函数。如果右键单击,它会运行 Quit 等菜单项。

    感谢之前回答的人!

    class AppDelegate: NSObject, NSApplicationDelegate {
      func applicationDidFinishedLaunching(_ notification: Notification) {
        // .....
        // .....
    
        if let button = statusItem?.button {
            button.image = NSImage(named: "iconImage")
            button.action = #selector(self.statusBarButtonClicked(_:))
            button.sendAction(on: [.leftMouseUp, .rightMouseUp])
        }
        // Add Menu Item for NSMenu
        menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
      }
      @objc func statusBarButtonClicked(_ sender: NSStatusBarButton) {
        let event = NSApp.currentEvent!
        if event.type == NSEvent.EventType.rightMouseUp {
          print("Right Click")
          statusItem?.menu = menu
          statusItem?.button?.performClick(nil)
          // statusItem?.popUpMenu(menu)
          statusItem?.menu = nil
        } else {
          print("Left Click")
          togglePopover(sender)
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多