Fix double-modifier bug in hotkey modifier sorting#146
Merged
iansan5653 merged 4 commits intomainfrom Mar 16, 2026
Merged
Conversation
iansan5653
commented
Mar 16, 2026
| Alt: 1, | ||
| Meta: 2, | ||
| Shift: 3 | ||
| } |
Member
Author
There was a problem hiding this comment.
It would be a little simpler to just write an ordered array here and use indexOf for the numbers, but it would be slightly slower since the lookup would be
Contributor
There was a problem hiding this comment.
Pull request overview
Updates hotkey normalization to better handle modifier-only shortcuts and adds test coverage for those cases.
Changes:
- Refactors modifier sorting in
normalizeHotkeyto use a general token sort with an explicit modifier order. - Adds normalizeHotkey tests for “only modifiers” and “modifiers + Mod” edge cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/test-normalize-hotkey.js | Adds new normalizeHotkey test cases for modifier-only inputs |
| src/hotkey.ts | Refactors modifier sorting logic used by normalizeHotkey |
Comments suppressed due to low confidence (1)
src/hotkey.ts:104
modifierKeyNamesalready defines the canonical modifier ordering, butorderedModifiersduplicates that list in a second place. To avoid these drifting apart in the future, consider generating the ordering map frommodifierKeyNames(single source of truth) instead of hardcoding both.
const modifierKeyNames: string[] = ['Control', 'Alt', 'Meta', 'Shift']
/**
* Normalizes a hotkey string before comparing it to the serialized event
* string produced by `eventToHotkeyString`.
* - Replaces the `Mod` modifier with `Meta` on mac, `Control` on other
* platforms.
* - Ensures modifiers are sorted in a consistent order
* @param hotkey a hotkey string
* @param platform NOTE: this param is only intended to be used to mock `navigator.platform` in tests. `window.navigator.platform` is used by default.
* @returns {string} normalized representation of the given hotkey string
*/
export function normalizeHotkey(hotkey: string, platform?: string | undefined): NormalizedHotkeyString {
let result: string
result = localizeMod(hotkey, platform)
result = sortModifiers(result)
return result as NormalizedHotkeyString
}
const matchApplePlatform = /Mac|iPod|iPhone|iPad/i
function localizeMod(hotkey: string, platform?: string | undefined): string {
const ssrSafeWindow = typeof window === 'undefined' ? undefined : window
const safePlatform = platform ?? ssrSafeWindow?.navigator.platform ?? ''
const localModifier = matchApplePlatform.test(safePlatform) ? 'Meta' : 'Control'
return hotkey.replace('Mod', localModifier)
}
const orderedModifiers: Partial<Record<string, number>> = {
Control: 0,
Alt: 1,
Meta: 2,
Shift: 3
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Member
Author
|
@copilot fix the eslint errors in the changed files |
Co-authored-by: iansan5653 <2294248+iansan5653@users.noreply.github.com>
Contributor
gracepark
approved these changes
Mar 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
sortModifersmethod duplicates a modifier if there is no non-modifier key. For example,sortModifiers("Alt")returns"Alt+Alt".This PR updates the method to preserve the input key names, limiting modifications to just sorting. Also updates tests.