Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Is there a way I can detect Key press and hold with MASShortcut? #148

Open
rampatra opened this issue Jul 11, 2020 · 4 comments
Open

Is there a way I can detect Key press and hold with MASShortcut? #148

rampatra opened this issue Jul 11, 2020 · 4 comments

Comments

@rampatra
Copy link

rampatra commented Jul 11, 2020

I want to perform some actions when the user presses and holds Fn key. Can I detect Key press and hold with this library? If yes, a simple code snippet would be great.

@rampatra rampatra changed the title Is there a way I can detect press and hold with MASShortcut? Is there a way I can detect Key press and hold with MASShortcut? Jul 11, 2020
@Kentzo
Copy link

Kentzo commented Jul 12, 2020

The answer is going to be different on whether your keypress is an ansi key-code (like A or ]) or a modifier flag (like Ctrl or Cmd).

AFAIK the underlying system API used by MASShortcut does not allow you to detect keycode-less shortcuts (like Fn). You may take a look at SRShortcutRecorder and specifically its SRAXGlobalShortcutMonitor. Note that this API requires Accessibility Permission.

As of ability to detect holds: neither MASShortcut nor SRShortcutRecorder supports it. That's an interesting feature though, please create an issue and detail your use-case.

@rampatra
Copy link
Author

The answer is going to be different on whether your keypress is an ansi key-code (like A or ]) or a modifier flag (like Ctrl or Cmd).

What if I say I am okay with using key having ansi key-code (like A or ])?

AFAIK the underlying system API used by MASShortcut does not allow you to detect keycode-less shortcuts (like Fn). You may take a look at SRShortcutRecorder and specifically its SRAXGlobalShortcutMonitor. Note that this API requires Accessibility Permission.

I have installed one app on my machine and one of its features works with long press Fn key and doesn't ask for accessibility permission. Not sure how are they doing it. Need to investigate more on this.

As of ability to detect holds: neither MASShortcut nor SRShortcutRecorder supports it. That's an interesting feature though, please create an issue and detail your use-case.

Can you pl tell me what more details you need? My use-case is that I want to toggle a feature of my app when a user presses and holds a key instead of just pressing once. That's all.

@Kentzo
Copy link

Kentzo commented Jul 12, 2020

Not sure how are they doing it. Need to investigate more on this.

If you only need to know when Fn is pressed and not prevent / alter how other apps see it, I think NSEvent. addGlobalMonitorForEvents should be sufficient to get notified when modifier flags change and act accordingly.

My use-case is that I want to toggle a feature of my app when a user presses and holds a key instead of just pressing once.

  1. Whether you need to toggle a keycode+modifier or modifier-only combination
  2. Whether you need run loop (as in RunLoop) to be blocked for the duration of the hold
  3. Whether you need to prevent other apps from seeing the event

If you answers are "modifier-only", "not blocking" and "no" then I'd use the API I mentioned above to set up a Timer or DispatchSourceTimer.

EDIT

When timer fires, just check modifier flags again. Or cancel the timer if modifier flags change before it gets a chance to fire.

@shpakovski
Copy link
Collaborator

@Kentzo Thanks for help!

@rampatra Don’t forget about System Preferences which may intercept fn:

fn

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

No branches or pull requests

3 participants