Skip to content

armgit / Handy

Public Imported from GitHub
0

A free, open source, and extensible speech-to-text application that works completely offline.

fix: emit parseable names for compound shortcut keys (#1862)

Brian McBrayer141f981d4dda
diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml
index 6dbcc221..80ada59f 100644
--- a/.github/workflows/code-quality.yml
+++ b/.github/workflows/code-quality.yml
@@ -52,6 +52,9 @@ jobs:
- name: Check model language coverage
run: bun run check:model-languages
+ - name: Test keyboard utilities
+ run: bun run test:keyboard
+
- name: Run ESLint
run: bun run lint
diff --git a/package.json b/package.json
index d5784b7e..6cba3c1f 100644
--- a/package.json
+++ b/package.json
@@ -14,6 +14,7 @@
"format:check": "prettier --check . && cd src-tauri && cargo fmt -- --check",
"format:frontend": "prettier --write .",
"format:backend": "cd src-tauri && cargo fmt",
+ "test:keyboard": "bun src/lib/utils/keyboard.test.ts",
"test:playwright": "playwright test",
"test:playwright:ui": "playwright test --ui",
"check:translations": "bun scripts/check-translations.ts",
diff --git a/src-tauri/src/shortcut/mod.rs b/src-tauri/src/shortcut/mod.rs
index 4cb6a5f0..6498cc0c 100644
--- a/src-tauri/src/shortcut/mod.rs
+++ b/src-tauri/src/shortcut/mod.rs
@@ -1403,3 +1403,24 @@ pub async fn get_available_accelerators() -> crate::managers::transcription::Ava
.await
.expect("get_available_accelerators panicked")
}
+
+#[cfg(test)]
+mod tests {
+ use handy_keys::Hotkey;
+ use tauri_plugin_global_shortcut::Shortcut;
+
+ #[test]
+ fn compound_shortcut_keys_parse_on_both_backends() {
+ for key in [
+ "scrolllock",
+ "capslock",
+ "numlock",
+ "pageup",
+ "pagedown",
+ "printscreen",
+ ] {
+ assert!(key.parse::<Shortcut>().is_ok(), "Tauri rejected {key}");
+ assert!(key.parse::<Hotkey>().is_ok(), "HandyKeys rejected {key}");
+ }
+ }
+}
diff --git a/src/lib/utils/keyboard.test.ts b/src/lib/utils/keyboard.test.ts
new file mode 100644
index 00000000..36bd17b5
--- /dev/null
+++ b/src/lib/utils/keyboard.test.ts
@@ -0,0 +1,27 @@
+import assert from "node:assert/strict";
+import { formatKeyCombination, getKeyName } from "./keyboard";
+
+const keyboardEvent = (value: { code?: string; key?: string }): KeyboardEvent =>
+ value as KeyboardEvent;
+
+const compoundKeys = [
+ ["ScrollLock", "scrolllock", "Scroll Lock"],
+ ["CapsLock", "capslock", "Caps Lock"],
+ ["NumLock", "numlock", "Num Lock"],
+ ["PageUp", "pageup", "Page Up"],
+ ["PageDown", "pagedown", "Page Down"],
+ ["PrintScreen", "printscreen", "Print Screen"],
+] as const;
+
+for (const [code, stored, displayed] of compoundKeys) {
+ assert.equal(getKeyName(keyboardEvent({ code })), stored);
+ assert.equal(formatKeyCombination(stored, "linux"), displayed);
+}
+
+assert.equal(getKeyName(keyboardEvent({ key: "CapsLock" })), "capslock");
+assert.equal(
+ getKeyName(keyboardEvent({ code: "AudioVolumeUp" })),
+ "audiovolumeup",
+);
+
+console.log("keyboard: all assertions passed");
diff --git a/src/lib/utils/keyboard.ts b/src/lib/utils/keyboard.ts
index c3de55b6..256d59f3 100644
--- a/src/lib/utils/keyboard.ts
+++ b/src/lib/utils/keyboard.ts
@@ -4,6 +4,16 @@
export type OSType = "macos" | "windows" | "linux" | "unknown";
+// Shortcut parsers require compact tokens; keep display labels separate.
+const COMPOUND_KEY_DISPLAY: Record<string, string> = {
+ capslock: "Caps Lock",
+ numlock: "Num Lock",
+ pageup: "Page Up",
+ pagedown: "Page Down",
+ printscreen: "Print Screen",
+ scrolllock: "Scroll Lock",
+};
+
/**
* Extract a consistent key name from a KeyboardEvent
* This function provides cross-platform keyboard event handling
@@ -66,7 +76,7 @@ export const getKeyName = (
MetaRight: getModifierName("meta"),
OSLeft: getModifierName("meta"),
OSRight: getModifierName("meta"),
- CapsLock: "caps lock",
+ CapsLock: "capslock",
Tab: "tab",
Enter: "enter",
Space: "space",
@@ -79,11 +89,11 @@ export const getKeyName = (
ArrowRight: "right",
Home: "home",
End: "end",
- PageUp: "page up",
- PageDown: "page down",
+ PageUp: "pageup",
+ PageDown: "pagedown",
Insert: "insert",
- PrintScreen: "print screen",
- ScrollLock: "scroll lock",
+ PrintScreen: "printscreen",
+ ScrollLock: "scrolllock",
Pause: "pause",
ContextMenu: "menu",
NumpadMultiply: "numpad *",
@@ -91,7 +101,7 @@ export const getKeyName = (
NumpadSubtract: "numpad -",
NumpadDecimal: "numpad .",
NumpadDivide: "numpad /",
- NumLock: "num lock",
+ NumLock: "numlock",
};
if (modifierMap[code]) {
@@ -117,8 +127,8 @@ export const getKeyName = (
return punctuationMap[code];
}
- // For any other codes, try to convert to a reasonable format
- return code.toLowerCase().replace(/([a-z])([A-Z])/g, "$1 $2");
+ // Parser names mirror KeyboardEvent.code without spaces.
+ return code.toLowerCase();
}
// Fallback to e.key if e.code is not available
@@ -134,7 +144,7 @@ export const getKeyName = (
osType === "macos" ? "command" : osType === "windows" ? "win" : "super",
OS:
osType === "macos" ? "command" : osType === "windows" ? "win" : "super",
- CapsLock: "caps lock",
+ CapsLock: "capslock",
ArrowUp: "up",
ArrowDown: "down",
ArrowLeft: "left",
@@ -164,6 +174,7 @@ const capitalizeKey = (key: string): string => {
if (/^f\d+$/.test(key)) return key.toUpperCase();
// Single char: a -> A
if (key.length === 1) return key.toUpperCase();
+ if (COMPOUND_KEY_DISPLAY[key]) return COMPOUND_KEY_DISPLAY[key];
// Multi-word: capitalize first letter of each word
return key.replace(/\b\w/g, (c) => c.toUpperCase());
};