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

How can I capture a system shortcut key? #139

Closed leontea closed 5 years ago

leontea commented 5 years ago

For example, I want to capture the cmd+Q, but the demo app just quit because cmd+Q is the shortcut key for quitting application.

zoul commented 5 years ago

There’s an explicit code path for Cmd+W and Cmd+Q handling in MASShortcutView:

// If the shortcut is Cmd-W or Cmd-Q, cancel recording and pass the event through
else if ((shortcut.modifierFlags == NSCommandKeyMask) && (shortcut.keyCode == kVK_ANSI_W || shortcut.keyCode == kVK_ANSI_Q)) {
    weakSelf.recording = NO;
}

When I comment that out and set a nil validator for _customShortcutView in the demo app, I can record and process Cmd+Q just fine. So it’s possible.

In theory, it would be nice for MASShortcutView to not dictate any shortcut filter at all, just offer a reasonable default behaviour that could be turned off. I have done just that in #127 with shortcut validation. It would make sense to do the same with Cmd+Q and Cmd-W handling. Perhaps a boolean toggle? I don’t have the time at the moment, would you take a stab at it, @leontea?

shpakovski commented 5 years ago

Thanks Tomaz, hi Leon. I agree that such functionality is quite advanced for a standard setup. @leontea Please consider to make a fork and suggest a Pull Request if you believe the component wins from that. Kind regards!

leontea commented 5 years ago

Thanks, Zoul.

I actually did find that disabling the validator could be doing the job.

But soon, I encounter another problem. I've set cmd+L as a LockScreen key in the Keyboard Preference, and the demo failed to capture it since my mac just got locked after pressing the key. It is completely customized and it could be anything else. I can't just put all the possibilities in the IF statement.

What I mean here is that I want to capture any shortcut key without triggering the action no matter whether it is already assigned something by the user.

shpakovski commented 5 years ago

What I mean here is that I want to capture any shortcut key without triggering the action no matter whether it is already assigned something by the user.

Leon, MASShortcut uses APIs which may sit on a higher level than some system shortcuts need. I believe hotkeys like Command-Tab must be treated differently and what you need here is a custom solution, additional to our component. Makes sense?

leontea commented 5 years ago

Thank you shpakovski. I have another question. How can I capture a single function key. For example, when I press the option key globally, I want my app to do something. I tried the following code, did not work. Could you give me some instructions?

MASShortcut *defaultShortcut = [MASShortcut shortcutWithKeyCode:58 modifierFlags:NSAlternateKeyMask];
[MASShortcut addGlobalHotkeyMonitorWithShortcut:defaultShortcut handler:^{
        NSLog(@"pressed option key!");
    }];
zoul commented 5 years ago

I think this is out of scope for MAS Shortcut, since the Option key itself is not a complete key shortcut. Take a look at event taps, they should be able to do what you want (monitor arbitrary keyboard events).

leontea commented 5 years ago

thanks a lot

zoul commented 5 years ago

You’re welcome! I will close this, but if you feel there is something more we could do for you, reopen or post a new issue.