KeyboardShortcuts icon indicating copy to clipboard operation
KeyboardShortcuts copied to clipboard

Hard-coded keyboard shortcuts

Open sindresorhus opened this issue 4 years ago • 8 comments

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.

sindresorhus avatar May 07 '20 16:05 sindresorhus

(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 avatar Jul 14 '20 15:07 jameshfisher

Aha, 5 minutes later, I stumbled on this merged PR which does exactly what I needed: https://github.com/sindresorhus/KeyboardShortcuts/pull/13

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

jameshfisher avatar Jul 14 '20 16:07 jameshfisher

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 avatar Jul 14 '20 16:07 sindresorhus

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.

sindresorhus avatar Jul 14 '20 16:07 sindresorhus

@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.

jameshfisher avatar Jul 14 '20 16:07 jameshfisher

I am adding a hard-codeed shortcut for my app. Below is my code:

static let someShortcut = Self("someShortcut", default: .init(.s, modifiers: [.command, .option]))
KeyboardShortcuts.onKeyDown(for: .someShortcut) {
   // Do something
}

But I can't activate this shortcut by pressing Command + Option + S. But it worked if I press Command + Shift + S.

I am sure there are no conflicting shortcuts because I have changed the combinations. And it also doesn't work if one of the modifiers is Control.

Is there something I missed?

francisfeng avatar Feb 25 '21 07:02 francisfeng

@francisfeng I tried adding your code to the example app and the shortcut works fine for me: https://github.com/sindresorhus/KeyboardShortcuts/commit/3c4577484b7390fac13b25f28e45bea57da3f960


In the future, open a new issue instead of commenting on a semi-related issue.

sindresorhus avatar Feb 26 '21 10:02 sindresorhus

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!

Vincz avatar Apr 09 '23 17:04 Vincz