Home > Software design >  Adding a shortcut that is already assigned to a button in SwiftUI view to macOS menu bar?
Adding a shortcut that is already assigned to a button in SwiftUI view to macOS menu bar?

Time:09-10

Let's say that I have a Button that I have assigned to a keyboard shortcut, like as shown:

Button("Example Button"){
 print("pressed")
}
.keyboardShortcut("b")

This will allow me to add a button that I can trigger by doing command B. However, this does not show up in macOS’s command bar at all, which is something I would like to add.

I have already considered using the .commands modifier and adding a command over there, but I would like to only manipulate a single window and I would be forced to use NotificationCenter or a centralized app storage variable, both of which manipulates all the windows of an app.

I have also attempted to use the commands thing with looking at which window is the key window, but that does not work reliably either and sometimes ends up manipulating the variables of multiple windows.

Is there a way to add a command bar shortcut for a Button that I have already assigned a keyboard shortcut to?

CodePudding user response:

Ultimately, I found out that there was a focusedSceneObject thing that I could've used in order to remedy this issue. However, this seems to only be available in SwiftUI 4.

  • Related