Skip to content

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

fix: restore previous hotkey when new registration fails (#220)

Gabriel Stein79c743efaa1c
diff --git a/src/helpers/hotkeyManager.js b/src/helpers/hotkeyManager.js
index 46d874f39..a3f5cc930 100644
--- a/src/helpers/hotkeyManager.js
+++ b/src/helpers/hotkeyManager.js
@@ -128,6 +128,8 @@ class HotkeyManager {
return { success: true, hotkey };
}
+ const previousHotkey = this.currentHotkey;
+
// Unregister the previous hotkey (skip native-listener-only hotkeys)
if (
this.currentHotkey &&
@@ -205,6 +207,8 @@ class HotkeyManager {
console.error(`[HotkeyManager] Failed to register hotkey: ${hotkey}`, failureInfo);
debugLogger.log(`[HotkeyManager] Registration failed:`, failureInfo);
+ this._restorePreviousHotkey(previousHotkey, callback);
+
let errorMessage = failureInfo.message;
if (failureInfo.suggestions.length > 0) {
errorMessage += ` Try: ${failureInfo.suggestions.join(", ")}`;
@@ -220,10 +224,41 @@ class HotkeyManager {
} catch (error) {
console.error("[HotkeyManager] Error setting up shortcuts:", error);
debugLogger.log(`[HotkeyManager] Exception during registration:`, error.message);
+ this._restorePreviousHotkey(previousHotkey, callback);
return { success: false, error: error.message };
}
}
+ _restorePreviousHotkey(previousHotkey, callback) {
+ if (
+ !previousHotkey ||
+ previousHotkey === "GLOBE" ||
+ isRightSideModifier(previousHotkey) ||
+ isModifierOnlyHotkey(previousHotkey)
+ ) {
+ return;
+ }
+ const prevAccel = previousHotkey.startsWith("Fn+")
+ ? previousHotkey.slice(3)
+ : previousHotkey;
+ try {
+ const restored = globalShortcut.register(prevAccel, callback);
+ if (restored) {
+ debugLogger.log(
+ `[HotkeyManager] Restored previous hotkey "${previousHotkey}" after failed registration`
+ );
+ } else {
+ debugLogger.warn(
+ `[HotkeyManager] Could not restore previous hotkey "${previousHotkey}"`
+ );
+ }
+ } catch (err) {
+ debugLogger.warn(
+ `[HotkeyManager] Exception restoring previous hotkey "${previousHotkey}": ${err.message}`
+ );
+ }
+ }
+
async initializeGnomeShortcuts(callback) {
if (process.platform !== "linux" || !GnomeShortcutManager.isWayland()) {
return false;
@@ -513,3 +548,4 @@ class HotkeyManager {
module.exports = HotkeyManager;
module.exports.isModifierOnlyHotkey = isModifierOnlyHotkey;
+module.exports.isRightSideModifier = isRightSideModifier;
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 1ff711fcc..12caa69f5 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -443,9 +443,9 @@ class IPCHandlers {
// When exiting capture mode with a new hotkey, use that to avoid reading stale state
const effectiveHotkey = !enabled && newHotkey ? newHotkey : hotkeyManager.getCurrentHotkey();
- const { isModifierOnlyHotkey } = require("./hotkeyManager");
+ const { isModifierOnlyHotkey, isRightSideModifier } = require("./hotkeyManager");
const usesNativeListener = (hotkey) =>
- !hotkey || hotkey === "GLOBE" || isModifierOnlyHotkey(hotkey);
+ !hotkey || hotkey === "GLOBE" || isModifierOnlyHotkey(hotkey) || isRightSideModifier(hotkey);
if (enabled) {
// Entering capture mode - unregister globalShortcut so it doesn't consume key events
@@ -475,12 +475,20 @@ class IPCHandlers {
// Exiting capture mode - re-register globalShortcut if not already registered
if (effectiveHotkey && !usesNativeListener(effectiveHotkey)) {
const { globalShortcut } = require("electron");
- if (!globalShortcut.isRegistered(effectiveHotkey)) {
+ const accelerator = effectiveHotkey.startsWith("Fn+")
+ ? effectiveHotkey.slice(3)
+ : effectiveHotkey;
+ if (!globalShortcut.isRegistered(accelerator)) {
debugLogger.log(
- `[IPC] Re-registering globalShortcut "${effectiveHotkey}" after capture mode`
+ `[IPC] Re-registering globalShortcut "${accelerator}" after capture mode`
);
const callback = this.windowManager.createHotkeyCallback();
- globalShortcut.register(effectiveHotkey, callback);
+ const registered = globalShortcut.register(accelerator, callback);
+ if (!registered) {
+ debugLogger.warn(
+ `[IPC] Failed to re-register globalShortcut "${accelerator}" after capture mode`
+ );
+ }
}
}