在菜单栏应用程序(Swift)中的OS X键事件 [英] OS X key event in menu bar app (Swift)

查看:106
本文介绍了在菜单栏应用程序(Swift)中的OS X键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在做一个Cocoa项目。它是一个菜单栏/状态栏专用应用程序。我需要一些额外的信息出现在菜单栏应用程序,当用户按下选项键,我一直在寻找无处不在,如果没有任何窗口显示在屏幕上这是可能的。是否可以运行 keyDown:从AppDelegate.swift来做这个?指导是赞赏。谢谢!

I've been working on a Cocoa project recently. It's a menu bar/status bar-only application. I need some extra information to appear in the menu bar app when the user holds down the option key, and I've been looking everywhere to see how this is possible without any windows showing onscreen. Is it possible to run keyDown: from AppDelegate.swift to do this? Guidance is appreciated. Thanks!

推荐答案

我不知道你是否可以全局监控键,但是你可以做的选项键如下: / p>

I am not sure if you can monitor the key down globally but the option key you can do as follow:

NSEvent.addGlobalMonitorForEventsMatchingMask(NSEventMask.FlagsChangedMask) { (theEvent) -> Void in
    if theEvent.modifierFlags.intersect(.DeviceIndependentModifierFlagsMask) == .AlternateKeyMask {
        print("ONLY OPTION")
    }
    switch theEvent.modifierFlags.intersect(.DeviceIndependentModifierFlagsMask) {
    case NSEventModifierFlags.ShiftKeyMask :
        print("shift key is pressed")
    case NSEventModifierFlags.ControlKeyMask:
        print("control key is pressed")
    case NSEventModifierFlags.AlternateKeyMask :
        print("option key is pressed")
    case NSEventModifierFlags.CommandKeyMask:
        print("Command key is pressed")
    default:
        print("no key or more than one is pressed")
    }
}

这篇关于在菜单栏应用程序(Swift)中的OS X键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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