Add a special `Mod` modifier that translates to either `Meta` on Mac or `Control` on other platforms
resolves #56
What is this?
On Mac, Meta (command) is frequently used as the modifier for shortcuts: on Windows and Linux Control is used instead. Therefor in order to correctly localize hotkeys, it's common to see code that handles swapping platform-specific modifier keys in users of this library. It would make sense to do this in the library and eliminate all that custom code!
Mod modifier
This PR adds support for using a special Mod modifier that automatically localizes to Control on Windows / Linux, or Meta on Mac.
The rational is that this allows an easy way to define cross-platform shortcuts. Without this feature, an implementer would have to either support both Control and Meta forms of shortcuts, or write their own code to handle swapping these.
Consistent sorting of modifiers
Although it was only documented in #62 , hotkey has always had a hard requirement that modifiers are sorted in the order that matches the order eventToHotkeyString formats the modifier keys from an event.
Since the Control and Meta keys appear in different places in the hotkey string, in order to support the Mod key, I also had to ensure that modifiers in a hotkey string are consistently sorted before comparoson to an event that is serialized via eventToHotkeyString.
So this PR also removes the hard requirement that modifiers be sorted in a strict order.
Approach
I chose to add a new normalizeHotkey function to handle localizing the Mod key and sorting the modifiers. This normalizeHotkey is applied to the hotkey string in the expandHotkeyToEdges before it is added to the Trie. This ensures that when we later look for a serialized event in the trie, the hotkey string is already normalized to match the serialized output of eventToHotkeyString.
Limitations
The "Mod" key is not currently compatible with Shift and Alt modifiers.
Because Mac treats event.key differently than Windows or Linux when the Shift and / or Alt key is pressed, it means that the Mod modifier will not localize as expected when paired with Shift. I have noted this limitation in the tests and in the README. I was hoping to address #54 and #68 in this PR too but decided to defer to keep the size of the PR down.
TODO
- [x] implement
normalizeHotkeyfunction to allow comparing aeventToHotkeystring to a hotkey string containing aModcharacter.- [x] Account for hotkeys that contain both
ControlandMeta! - [ ] Account for how Mac lowercases
event.keywhenMeta+Shiftare pressed.
- [x] Account for hotkeys that contain both
- [x] Make
normalizeHotkeyplatform specific: convertModtoMetaon Mac orControlon other platforms. - [x] Use this
normalizeHotkeyto map hotkey to Trie inexpandHotkeyToEdgesso that the Trie contains the platform-specific modifier: use it to allowkeyDownHandlerto match events to hotkey strings containing theModalias. - [x] Add docs about
Modcharacter- [x] Add special notes about
Shiftetc?
- [x] Add special notes about