Skip to content

Convert NSEventModifierFlags to a symbolic representation #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kopyl opened this issue Mar 11, 2025 · 6 comments
Open

Convert NSEventModifierFlags to a symbolic representation #204

kopyl opened this issue Mar 11, 2025 · 6 comments

Comments

@kopyl
Copy link

kopyl commented Mar 11, 2025

Do you know how I can safely convert a modifier from KeyboardShortcuts.Name.openTabsList.shortcut.modifiers to say "⇥"?

@kopyl
Copy link
Author

kopyl commented Mar 11, 2025

I mean how safe would this be?

func modifierFlagsToSymbols(_ flags: NSEvent.ModifierFlags) -> String {
    var symbols = ""
    
    if flags.contains(.control) {
        symbols += "⌃"
    }
    if flags.contains(.option) {
        symbols += "⌥"
    }
    if flags.contains(.shift) {
        symbols += "⇧"
    }
    if flags.contains(.command) {
        symbols += "⌘"
    }
    if flags.contains(.function) {
        symbols += "fn"
    }
    
    return symbols
}

@kopyl
Copy link
Author

kopyl commented Mar 11, 2025

Or something a bit cleaner

extension NSEvent.ModifierFlags {
    var symbolRepresentation: String {
        var symbols = ""

        if contains(.command) {
            symbols += "⌘"
        }
        if contains(.option) {
            symbols += "⌥"
        }
        if contains(.control) {
            symbols += "⌃"
        }
        if contains(.shift) {
            symbols += "⇧"
        }
        if contains(.capsLock) {
            symbols += "⇪"
        }

        return symbols
    }
}

To use it like
KeyboardShortcuts.Name.openTabsList.shortcut?.modifiers.symbolRepresentation

@sindresorhus
Copy link
Owner

That looks fine, but you need to ensure the order is correct (that it follows the order macOS would put the symbols). We could add something for this here, but I want to avoid extending built-in types as it could cause conflicts with user's own extensions.

@kopyl
Copy link
Author

kopyl commented Mar 13, 2025

@sindresorhus thank you.

By the way, I just used you library in one of my apps

https://apps.apple.com/us/app/tab-finder/id6741719894

Initially I just used HotKey library, but then users started asking to implement custom shortcuts and I looked into your library.

Initially I tried yours before using HotKey, but it was unable to add to my packages for some reason (it fixed now).

@kopyl
Copy link
Author

kopyl commented Mar 16, 2025

@sindresorhus you could add the list of apps which use this package to the README.

@sindresorhus
Copy link
Owner

No, it requires too much maintenance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants