Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(windows): stabilize native hotkeys and recorder clicks

Gabriel Stein98c59daa6aee
diff --git a/resources/windows-key-listener.c b/resources/windows-key-listener.c
index a40ae2bd0..2166c189c 100644
--- a/resources/windows-key-listener.c
+++ b/resources/windows-key-listener.c
@@ -25,17 +25,68 @@ static BOOL g_requireAlt = FALSE;
static BOOL g_requireShift = FALSE;
static BOOL g_requireWin = FALSE;
static BOOL g_useModifiersOnly = FALSE;
+static BOOL g_ctrlDown = FALSE;
+static BOOL g_altDown = FALSE;
+static BOOL g_shiftDown = FALSE;
+static BOOL g_leftWinDown = FALSE;
+static BOOL g_rightWinDown = FALSE;
+
+static BOOL IsCtrlVk(DWORD vkCode) {
+ return vkCode == VK_CONTROL || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL;
+}
+
+static BOOL IsAltVk(DWORD vkCode) {
+ return vkCode == VK_MENU || vkCode == VK_LMENU || vkCode == VK_RMENU;
+}
+
+static BOOL IsShiftVk(DWORD vkCode) {
+ return vkCode == VK_SHIFT || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT;
+}
+
+static BOOL IsWinVk(DWORD vkCode) {
+ return vkCode == VK_LWIN || vkCode == VK_RWIN;
+}
+
+static void UpdateModifierState(DWORD vkCode, BOOL isKeyDown) {
+ if (IsCtrlVk(vkCode)) {
+ g_ctrlDown = isKeyDown;
+ return;
+ }
+
+ if (IsAltVk(vkCode)) {
+ g_altDown = isKeyDown;
+ return;
+ }
+
+ if (IsShiftVk(vkCode)) {
+ g_shiftDown = isKeyDown;
+ return;
+ }
-// Check if required modifiers are currently pressed
-BOOL AreModifiersPressed(void) {
- if (g_requireCtrl && !(GetAsyncKeyState(VK_CONTROL) & 0x8000)) return FALSE;
- if (g_requireAlt && !(GetAsyncKeyState(VK_MENU) & 0x8000)) return FALSE;
- if (g_requireShift && !(GetAsyncKeyState(VK_SHIFT) & 0x8000)) return FALSE;
- if (g_requireWin &&
- !(GetAsyncKeyState(VK_LWIN) & 0x8000) &&
- !(GetAsyncKeyState(VK_RWIN) & 0x8000)) {
- return FALSE;
+ if (vkCode == VK_LWIN) {
+ g_leftWinDown = isKeyDown;
+ return;
}
+
+ if (vkCode == VK_RWIN) {
+ g_rightWinDown = isKeyDown;
+ }
+}
+
+static BOOL IsRequiredModifierEvent(DWORD vkCode) {
+ return (g_requireCtrl && IsCtrlVk(vkCode)) ||
+ (g_requireAlt && IsAltVk(vkCode)) ||
+ (g_requireShift && IsShiftVk(vkCode)) ||
+ (g_requireWin && IsWinVk(vkCode));
+}
+
+// Track modifier state inside the hook instead of using GetAsyncKeyState().
+// For low-level hooks, async state is not yet updated for the current event.
+static BOOL AreRequiredModifiersPressed(void) {
+ if (g_requireCtrl && !g_ctrlDown) return FALSE;
+ if (g_requireAlt && !g_altDown) return FALSE;
+ if (g_requireShift && !g_shiftDown) return FALSE;
+ if (g_requireWin && !(g_leftWinDown || g_rightWinDown)) return FALSE;
return TRUE;
}
@@ -127,38 +178,30 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
KBDLLHOOKSTRUCT* kbd = (KBDLLHOOKSTRUCT*)lParam;
BOOL isKeyDown = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);
BOOL isKeyUp = (wParam == WM_KEYUP || wParam == WM_SYSKEYUP);
+ BOOL isModifierEvent = IsCtrlVk(kbd->vkCode) || IsAltVk(kbd->vkCode) ||
+ IsShiftVk(kbd->vkCode) || IsWinVk(kbd->vkCode);
- // Check if a required modifier was released while we're in key-down state
- if (g_isKeyDown && isKeyUp) {
- BOOL modifierReleased = FALSE;
- if (g_requireCtrl && (kbd->vkCode == VK_CONTROL || kbd->vkCode == VK_LCONTROL || kbd->vkCode == VK_RCONTROL)) {
- modifierReleased = TRUE;
- }
- if (g_requireAlt && (kbd->vkCode == VK_MENU || kbd->vkCode == VK_LMENU || kbd->vkCode == VK_RMENU)) {
- modifierReleased = TRUE;
- }
- if (g_requireShift && (kbd->vkCode == VK_SHIFT || kbd->vkCode == VK_LSHIFT || kbd->vkCode == VK_RSHIFT)) {
- modifierReleased = TRUE;
- }
- if (g_requireWin && (kbd->vkCode == VK_LWIN || kbd->vkCode == VK_RWIN)) {
- modifierReleased = TRUE;
- }
- if (modifierReleased) {
- g_isKeyDown = FALSE;
- printf("KEY_UP\n");
- fflush(stdout);
- }
+ if ((isKeyDown || isKeyUp) && isModifierEvent) {
+ UpdateModifierState(kbd->vkCode, isKeyDown);
+ }
+
+ // Stop an active press as soon as one of its required modifiers is released.
+ if (g_isKeyDown && isKeyUp && IsRequiredModifierEvent(kbd->vkCode) &&
+ !AreRequiredModifiersPressed()) {
+ g_isKeyDown = FALSE;
+ printf("KEY_UP\n");
+ fflush(stdout);
}
if (g_useModifiersOnly) {
if (isKeyDown) {
- if (!g_isKeyDown && AreModifiersPressed()) {
+ if (!g_isKeyDown && AreRequiredModifiersPressed()) {
g_isKeyDown = TRUE;
printf("KEY_DOWN\n");
fflush(stdout);
}
} else if (isKeyUp) {
- if (g_isKeyDown && !AreModifiersPressed()) {
+ if (g_isKeyDown && !AreRequiredModifiersPressed()) {
g_isKeyDown = FALSE;
printf("KEY_UP\n");
fflush(stdout);
@@ -171,7 +214,7 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (kbd->vkCode == g_targetVk) {
if (isKeyDown) {
// Only trigger if modifiers are satisfied and not already down
- if (!g_isKeyDown && AreModifiersPressed()) {
+ if (!g_isKeyDown && AreRequiredModifiersPressed()) {
g_isKeyDown = TRUE;
printf("KEY_DOWN\n");
fflush(stdout);
diff --git a/scripts/build-windows-key-listener.js b/scripts/build-windows-key-listener.js
index 3f222382a..a8497a254 100644
--- a/scripts/build-windows-key-listener.js
+++ b/scripts/build-windows-key-listener.js
@@ -4,8 +4,8 @@
*
* Strategy:
* 1. If binary exists and is up-to-date, do nothing
- * 2. Try to download prebuilt binary from GitHub releases
- * 3. Fall back to local compilation if download fails
+ * 2. Try to compile locally so source changes are picked up in Windows builds
+ * 3. Fall back to downloading a prebuilt binary if local compilation is unavailable
*
* This allows developers without a C compiler to still build the app.
*/
@@ -115,7 +115,8 @@ function tryCompile() {
check: { command: "gcc", args: ["--version"] },
useShell: false,
command: "gcc",
- args: ["-O2", "-mwindows", cSource, "-o", outputBinary, "-luser32"],
+ // Keep the console subsystem so stdout/stderr remain attached to the parent process.
+ args: ["-O2", cSource, "-o", outputBinary, "-luser32"],
},
// Clang (LLVM) - can use shell: false
{
@@ -179,14 +180,13 @@ async function main() {
return;
}
- // Try download first, then compile
- const downloaded = await tryDownload();
- if (downloaded) {
+ const compiled = tryCompile();
+ if (compiled) {
return;
}
- const compiled = tryCompile();
- if (compiled) {
+ const downloaded = await tryDownload();
+ if (downloaded) {
return;
}
diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.js
index 8072ddf24..0e4feab7c 100644
--- a/src/helpers/windowManager.js
+++ b/src/helpers/windowManager.js
@@ -27,7 +27,6 @@ class WindowManager {
this.hotkeyManager = new HotkeyManager();
this.dragManager = new DragManager();
this.isQuitting = false;
- this.isMainWindowInteractive = false;
this.loadErrorShown = false;
this.macCompoundPushState = null;
this.winPushState = null;
@@ -98,12 +97,18 @@ class WindowManager {
return;
}
+ if (process.platform === "win32") {
+ // Windows click-through forwarding is unreliable for this floating panel.
+ // Keep the panel interactive so the mic button and cancel button are always clickable.
+ this.mainWindow.setIgnoreMouseEvents(false);
+ return;
+ }
+
if (shouldCapture) {
this.mainWindow.setIgnoreMouseEvents(false);
} else {
this.mainWindow.setIgnoreMouseEvents(true, { forward: true });
}
- this.isMainWindowInteractive = shouldCapture;
}
resizeMainWindow(sizeKey) {
@@ -908,7 +913,6 @@ class WindowManager {
this.mainWindow.on("closed", () => {
this.dragManager.cleanup();
this.mainWindow = null;
- this.isMainWindowInteractive = false;
});
}