检测NSStatusItem(Swift)上的左右点击事件 [英] Detect left and right click events on NSStatusItem (Swift)

查看:232
本文介绍了检测NSStatusItem(Swift)上的左右点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个状态栏应用程序,并希望根据用户单击左还是右来调用不同的操作.这是我到目前为止的内容:

I’m building a status bar app and want to call different actions depending on if the user clicked left or right. Here’s what I have so far:

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

未调用我的函数,也找不到原因.感谢您的帮助!

My function is not called and I couldn’t find our why. I appreciate any help!

推荐答案

您是否尝试过:

button.sendAction(on: [.leftMouseUp, .rightMouseUp])

然后看到在doSomeAction()功能中按下了哪个鼠标按钮?

Then seeing which mouse button was pressed in the doSomeAction() function?

所以看起来像...

let statusItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength)

func applicationDidFinishLaunching(_ aNotification: Notification) {

    if let button = statusItem.button {
        button.action = #selector(self.doSomeAction(sender:))
        button.sendAction(on: [.leftMouseUp, .rightMouseUp])
    }

}

func doSomeAction(sender: NSStatusItem) {

    let event = NSApp.currentEvent!

    if event.type == NSEventType.rightMouseUp {
        // Right button click
    } else {
        // Left button click
    }

}

https://github.com/craigfrancis/datetime/blob/master/xcode/DateTime/AppDelegate.swift

这篇关于检测NSStatusItem(Swift)上的左右点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆