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

Is there a way to detect if modifier keys still pressed on key up? #89

Closed
sarunw opened this issue Jul 7, 2022 · 3 comments
Closed

Comments

@sarunw
Copy link

sarunw commented Jul 7, 2022

Right now KeyboardShortcuts.onKeyUp recognize key up when users release non-modifier keys.

Is there a way to detect whether modifier keys are still pressed or not?

I want to replicate the "command+tab" behavior where users can cycle through items while modifier keys still down.

@sindresorhus
Copy link
Owner

There's no built-in support for this. Most use-cases for Command+Tab is for local events, where you don't need this package.

You could do it yourself though. Just use https://swiftpackageindex.com/sindresorhus/keyboardshortcuts/main/documentation/keyboardshortcuts/keyboardshortcuts/events(for:) and make an interval timer on key down and stop it on key up.

@sarunw
Copy link
Author

sarunw commented Jul 8, 2022

Thanks for the prompt response, but I don't understand the solution provided 😅

Let's say I want to replicate the functionality of Command+Tab with Command+Control+Tab.

After Command+Control+Tab is recognized, what should I check in the timer?

This is the function I try to replicate.

  1. After Command+Tab, I want to show opened app.
  2. While holding Command, you can release Tab.
  3. Press Tab again would move the selection.
  4. Release both Command and Tab will select and dismiss the window.

CleanShot 2022-07-08 at 07 10 57

@sindresorhus
Copy link
Owner

You could maybe register Command+Tab as a global keyboard shortcut, and then have a check for whether the Command key is still pressed, and hide the window if not.

Or you could check for NSEvent.modifierFlags.contains(.command) in keyUp.

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2022
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