swift2 左击与右击状态栏项目Mac Swift 2

luaexgnf  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(234)

我一直在尝试开发一个简单的程序,它位于Mac的状态栏中。我需要它,如果你左键单击,它会运行一个函数,但如果你右键单击,它会显示一个带有“关于”和“退出”项的菜单。
我一直在寻找,但所有我能找到的是命令或控制点击建议,但我宁愿不去这条路线。
提前表示感谢,任何帮助都将不胜感激!

zazmityj

zazmityj1#

雨燕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/上找到

v64noz0r

v64noz0r2#

为此,您可以使用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
    }
}
rqcrx0a6

rqcrx0a63#

在雨燕5中,
我是这样工作的。我能够从上面的答案中检测出它是右键单击还是左键单击。
但是,右键单击后,只能始终显示NSMenu。
然后,将statusItem.button?.performClick(nil)statusItem?.menu = nil相加
您可以添加statusItem?.popUpMenu(menu),但它在macOS 10.14中会贬值。(但它运行得很好。)
最后,如果左键单击,它会运行一个函数。如果右键单击,它会运行像退出这样的菜单项。
感谢之前回答的人!

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)
    }
  }
}

相关问题