6

I'm trying to programmatically open MenuBarExtra in Swift on macOS using a global keyboard shortcut. I've already setup all the code for the keyboard shortcut but I can't figure out how to show/hide the MenuBarExtra using SwiftUI. Is there a way of doing so in native SwiftUI which I can't seem to find? Otherwise, is there a way of doing so via AppKit?

I've defined my menubar app the in the following way:

MenuBarExtra("Title", image: "sample_icon", content: {
      ContentView()
         .preferredColorScheme(.dark)
})

1 Answer 1

3

The following worked for me:

I'm using HotKey (https://github.com/soffes/HotKey )

My keyUpHandler looks like this:

{
let statusItem = NSApp.windows.first?.value(forKey: "statusItem") as? NSStatusItem
statusItem?.button?.performClick(nil)
}

Works for both .menu and .window menuBarExtraStyle

Sign up to request clarification or add additional context in comments.

1 Comment

This solution helped me very much. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.