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!
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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") } }