检查菜单栏应用程序上的按键
Checking for key down on menu bar application
我正在编写一个使用 popover
的 MacOS 菜单栏应用程序。我依靠许多教程来进行操作。
非常简单,代码如下所示:
class AppDelegate: NSObject, NSApplicationDelegate {
var popover=NSPopover()
var statusBarItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Popover & Content View
let contentView = ContentView()
self.popover.contentViewController = NSHostingController(rootView: contentView)
// Menu
self.statusBarItem = NSStatusBar.system.statusItem(withLength: 18)
if let statusBarButton = self.statusBarItem.button {
statusBarButton.title = "☰"
statusBarButton.action = #selector(togglePopover(_:))
}
}
@objc func togglePopover(_ sender: AnyObject?) {
let statusBarButton=self.statusBarItem.button!
func show(_ sender: AnyObject) {
self.popover.show(relativeTo: statusBarButton.bounds, of: statusBarButton, preferredEdge: NSRectEdge.maxY)
}
func hide(_ sender: AnyObject) {
popover.performClose(sender)
}
self.popover.isShown ? hide(sender as AnyObject) : show(sender as AnyObject)
}
}
如何检查单击菜单按钮时选项键是否按下?
询问当前事件修饰符flags是否包含option
func isOptionkeyPressed() -> Bool
{
return NSApp.currentEvent?.modifierFlags.contains(.option) == true
}
我正在编写一个使用 popover
的 MacOS 菜单栏应用程序。我依靠许多教程来进行操作。
非常简单,代码如下所示:
class AppDelegate: NSObject, NSApplicationDelegate {
var popover=NSPopover()
var statusBarItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Popover & Content View
let contentView = ContentView()
self.popover.contentViewController = NSHostingController(rootView: contentView)
// Menu
self.statusBarItem = NSStatusBar.system.statusItem(withLength: 18)
if let statusBarButton = self.statusBarItem.button {
statusBarButton.title = "☰"
statusBarButton.action = #selector(togglePopover(_:))
}
}
@objc func togglePopover(_ sender: AnyObject?) {
let statusBarButton=self.statusBarItem.button!
func show(_ sender: AnyObject) {
self.popover.show(relativeTo: statusBarButton.bounds, of: statusBarButton, preferredEdge: NSRectEdge.maxY)
}
func hide(_ sender: AnyObject) {
popover.performClose(sender)
}
self.popover.isShown ? hide(sender as AnyObject) : show(sender as AnyObject)
}
}
如何检查单击菜单按钮时选项键是否按下?
询问当前事件修饰符flags是否包含option
func isOptionkeyPressed() -> Bool
{
return NSApp.currentEvent?.modifierFlags.contains(.option) == true
}