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:
@@ -26,7 +26,12 @@ function keyDisplayName(code: string, key: string): string {
|
||||
}
|
||||
|
||||
function codeToNumeric(code: string): number {
|
||||
return code.charCodeAt(0) * 256 + (code.charCodeAt(1) || 0);
|
||||
// djb2 hash — produces unique numeric IDs for all KeyboardEvent.code values
|
||||
let hash = 5381;
|
||||
for (let i = 0; i < code.length; i++) {
|
||||
hash = ((hash << 5) + hash + code.charCodeAt(i)) | 0;
|
||||
}
|
||||
return hash >>> 0; // ensure unsigned
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user