Question about a pattern I should use

Hello there,
sorry if this place is not intended for noobs like me. English is not by first language and so I cannot find a proper answer to my question on the Internet.

  1. I want to create a MacOS app that is heavily keyboard-oriented (global hotkeys mostly).
  2. For that, to support actions like remappable hotkeys I wanted to use the Command pattern however I am lost.
    2.1. how can I then serialize the command so that it can be bound to a proper hotkey?
    2.2. How can I even realize global hotkeys? I know there's the Hotkey package, but its code is a bit complicated for me and I feel bad by using code I don't understand, plus it seems to rely on the Carbon API, which is deprecated.
  3. A question related to the others, what is the best way to subscribe to system notifications, such as clipboard changes? I need it not in the UI itself, but in the part that will handle the keyboard operation.

Are you just trying to accept keyboard commands?

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            Button("Do something") {
                print("Did something")
            }
                .keyboardShortcut("B", modifiers: .command)
        }
    }
}

This app will print "Did something" to the console when the user presses command+B and the app is in focus

1 Like

Hey there,
I don't mean local shortcuts, by global hotkeys I mean hotkeys that can be performed even when the app is not in the focus, running in the menu bar, for instance.

This question is better asked on Apple dev forums as this forum tries to stay focused on Swift language itself. When you ask there split the question in two as they are two separate and unrelated questions.


With that being said, I don't see RegisterEventHotKey marked deprecated in the header (while there is other stuff that's marked deprecated in that very header), so it should probably work, OTOH I don't see RegisterEventHotKey in the documentation either that's a telling sign it is on the brink of deprecation even if works now. And quick looking at NSPasteboard API suggests it doesn't have the relevant change notification (which you have in UIPasteboard), so polling for changeCount changes from a timer would be the only answer.

Hi there,
I am sorry. I have seen some questions regarding SwiftUI so I have decided to ask nonetheless. OK, I will look further.

I did this years ago and think I used EventTaps

Possibly misremembering and just researched EventTaps but it looks like they should work

tera wrote:

This question is better asked on Apple dev forums

Right. Specifically, I can help you with this:

How can I even realize global hotkeys?

but it’s way off topic for Swift Forums. Please start a new thread over on Apple Developer Forums. Tag it with Privacy so that I see it [1].

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

[1] That may seem like an odd choice but it’ll make sense, trust me (-:

Now I know, this is hardcore OP but Apple Development Forum is dreadful to use with VoiceOver on MacOS and I am blind so I have no other way. Ahhh, let me try. And I am yet again sorry for the off topic I made.

OK, I have submitted the question to the appropriate place. Thank you for your help.

Apple Development Forum is dreadful to use with VoiceOver on macOS

Hmmm, that’s not good. I’d appreciate you filing a bug describing your experience with DevForums and VoiceOver. I’ll make sure that gets seen by the right folks.

Please post your bug number, just for the record.

Regarding your technical issue, I’ll reply to your DevForums thread later today.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

2 Likes