Skip to content
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

Hard-coded keyboard shortcuts #2

Open
sindresorhus opened this issue May 7, 2020 · 8 comments
Open

Hard-coded keyboard shortcuts #2

sindresorhus opened this issue May 7, 2020 · 8 comments

Comments

@sindresorhus
Copy link
Owner

Sometimes you need to add hard-coded keyboard shortcuts. Would be useful to add an API for that.

It could be an overload to onKeyUp/onKeyDown that accepts a Key and NSEvent.ModifierFlags.

KeyboardShortcuts.onKeyUp(.a, modifiers: [.command]) {
	// Do something.
}

Alternatively, it could just support a KeyboardShortcuts.Shortcut type directly:

KeyboardShortcuts.onKeyUp(.init(.a, modifiers: [.command])) {
	// Do something.
}

We should document that devs should prefer to let users customize the shortcuts instead.

Feedback wanted.

@jameshfisher
Copy link

(First off, thanks for this library - it looks great!)

I believe this issue is describing what I was looking for. In my app, I need to be able to say:

To pause or unpause, press Fn+Ctrl+p. If you prefer a different shortcut, you can customize it in Preferences.

I can't see a way to implement that with the current KeyboardShortcuts API. It seems to require the user to provide a custom Shortcut for a Name before it can be used.

I can see that the API lets me construct a KeyboardShortcuts.Shortcut, e.g.

KeyboardShortcuts.Shortcut(.p, modifiers: [.function, .control])

However, I can't do anything with this shortcut, so I'm not sure why it's available in the API at the moment.

I'm considering hacking around this by reading and writing to UserDefaults directly, but that seems ugly and brittle.

I'm happy to work on the implementation, assuming I've understood correctly so far.

@jameshfisher
Copy link

Aha, 5 minutes later, I stumbled on this merged PR which does exactly what I needed: #13

It's not documented, though. I can update the docs, if you wish.

@sindresorhus
Copy link
Owner Author

It's not documented, though. I can update the docs, if you wish.

It is documented: https://sindresorhus.com/KeyboardShortcuts/Enums/KeyboardShortcuts/Name.html#/s:17KeyboardShortcutsAAO4NameV_7defaultADSS_AB8ShortcutVSgtcfc

@sindresorhus
Copy link
Owner Author

To pause or unpause, press Fn+Ctrl+p. If you prefer a different shortcut, you can customize it in Preferences.

Keep this in mind though:

Users find it annoying when random apps steal their existing keyboard shortcuts. It’s generally better to show a welcome screen on the first app launch that lets the user set the shortcut.

@jameshfisher
Copy link

@sindresorhus ah, yes, I meant the README, which is the main place I was looking 😳

The warning makes sense. In my case, I'm migrating existing behavior, and the app is session-based rather than a long-lived menu bar app, which I think makes it less annoying.

@francisfeng

This comment has been minimized.

@sindresorhus

This comment has been minimized.

@Vincz
Copy link

Vincz commented Apr 9, 2023

Hi @sindresorhus!
Any update about this? The library is amazing, but I'd like to use it without the UserDefaults stuff.
I think the new API you suggest would solve a large part of what I want to do.

Thanks!

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

4 participants