cocoabits / MASShortcut

Modern framework for managing global keyboard shortcuts compatible with Mac App Store. More details:
http://blog.shpakovski.com/2012/07/global-keyboard-shortcuts-in-cocoa.html
BSD 2-Clause "Simplified" License
1.52k stars 220 forks source link

Execute action on key down too? #107

Open yn5 opened 7 years ago

yn5 commented 7 years ago

Is there a way to make the make the action execute on both key down and key up?

zoul commented 7 years ago

Sorry, not at the moment. (See MASShortcutMonitor, we look for the kEventHotKeyPressed event.) It should be trivial to patch the code yourself to watch both kEventHotKeyPressed and kEventHotKeyReleased, but I think the feature would be used so seldom it’s hard to justify adding it to MASShortcut. What’s your exact use case?

yn5 commented 7 years ago

Thanks for the info @zoul! I'm writing an application that is momentarily activated while a hotkey is held, so active when pressed, inactive when released.

zoul commented 7 years ago

Ah, that makes good sense. In that case I suggest writing your own MASShortcutMonitor class (it should be fairly simple to adapt it from the vanilla one) and plugging it into a MASShortcutBinder, maybe even the shared one. Then you should get what you want while still keeping the rest of MASShortcut. Do you think that would work for you?

yn5 commented 7 years ago

Thanks a lot for your suggestion @zoul! I'm pretty new to macOS/Cocoa development though and I'm not entirely sure if I understand you correctly. Should I write a new MASShortcutMonitor (not subclassing the existing one) and plug it into MASShortcutBinder using its setShortcutMonitor method?

In that case I’d think it would be easier for me to fork the repo and modify the existing MASShortcutMonitor to allow for the functionality. Or am I wrong thinking that or wrong in my understanding of your suggestion altogether?

zoul commented 7 years ago

You got my suggestion just right! The advantage in comparison to forking the repo is that you could keep using vanilla MASShortcut (except the monitor), getting new bug fixes, features etc. (But that’s just the first thing that came to my mind when thinking about your problem, so there might be an obvious downside somewhere.) I think we could even make MASShortcutMonitor easier to modify, but I don’t have the time right now.

yn5 commented 7 years ago

Cool! I'll give it a shot once I have time one of the coming days.

yn5 commented 7 years ago

@zoul I gave that shot in: https://github.com/shpakovski/MASShortcut/pull/111