Set single key shortcut
soundflix opened this issue · 5 comments
I would like to set shortcut keys without modifiers. Is it possible with KeyboardShortcuts??
I don't think that's possible. The low-level API we use expects both a key and modifier. Even if it were possible, it's not something I would want to support here. It's a niche use-case and it's easy to misuse it. You generally should not take over keys like that globally without a modifier.
For your use-case, it check out addGlobalMonitorForEvents(matching:handler:)
(note that it requires accessibility permission).
If your use-case is to capture media keys, see: https://github.com/nhurden/MediaKeyTap
It is possible with RegisterEventHotKey
, and it works in a playground.
In Apple docRegisterEventHotKey
I see:
- inHotKeyModifiers:
-
The keyboard modifiers to look for. On Mac OS X 10.2 or
-
earlier, this parameter must be non-zero, i.e. there must be a
-
modifier specified. On Mac OS X 10.3 or later, you may pass
-
zero.
However, when I try to modify your framework to accept 'no modifier', I get error -9868 when calling RegisterEventHotKey
.
CarbonEventsCore.h
says it's a "A generic error".
I understand you don't want this in your framework, but could you give me a clue?
Sorry, I have no idea.
Ok, one step further, I have put my plain RegisterEventHotKey
sample code in a CLI and it works.
I put the same in an AppKit app (will check SwiftUI later) and it gives me error -9868.
Ok, I found the solution is pretty simple: The App cannot be sandboxed. Then all keys work.