fix: replace broken 2-char hash with djb2 for keybind code mapping

The previous hash (charCodeAt(0)*256 + charCodeAt(1)) only used the
first 2 characters of KeyboardEvent.code strings, causing all Key*
codes (KeyA, KeyB, ..., KeyZ) to collide to the same numeric value.
This made Ctrl+D and Ctrl+M appear as identical bindings (false
conflict) and broke keybind matching in the web fallback.

djb2 hashes the full string, producing unique values for all codes.
This commit is contained in:
Jannis Braun
2026-03-22 21:01:02 +01:00
parent 6ca28765bc
commit 5554b1abc7
2 changed files with 12 additions and 4 deletions
+6 -3
View File
@@ -83,9 +83,12 @@ function setupWebFallback(keybindsRef: React.MutableRefObject<Keybind[]>): WebCl
const activeActions = new Set<string>();
function browserCodeToUiohook(code: string): number {
// Stable numeric ID from KeyboardEvent.code — consistent within web context
// because the recorder captures using the same mapping
return code.charCodeAt(0) * 256 + (code.charCodeAt(1) || 0);
// djb2 hash — must match codeToNumeric() in KeybindsPanel.tsx
let hash = 5381;
for (let i = 0; i < code.length; i++) {
hash = ((hash << 5) + hash + code.charCodeAt(i)) | 0;
}
return hash >>> 0;
}
function checkKeybinds(isDown: boolean): void {