Prevents global hotkey handling during recording

Improves shortcut editing experience by disabling global hotkey interception while a keybinding is being recorded.
Ensures that hotkey recording state persists and synchronizes across settings and UI.
Prevents conflicts between shortcut editor and app-level key handling.
This commit is contained in:
bincxz committed 2025-12-19 07:03:00 +08:00
1 parent e93ddf2bd3
commit 9d4d349329
6 files changed
+66 -11

No files matched your search

+10 -3
View File
@@ -153,6 +153,7 @@ function App({ settings }: { settings: SettingsState }) {
terminalSettings,
hotkeyScheme,
keyBindings,
isHotkeyRecording,
} = settings;
const {
@@ -427,7 +428,7 @@ function App({ settings }: { settings: SettingsState }) {
// Global hotkey handler
useEffect(() => {
if (hotkeyScheme === 'disabled') return;
if (hotkeyScheme === 'disabled' || isHotkeyRecording) return;
const isMac = hotkeyScheme === 'mac';
if (IS_DEV) console.log('[Hotkeys] Registering global hotkey handler, scheme:', hotkeyScheme, 'bindings count:', keyBindings.length);
@@ -442,6 +443,9 @@ function App({ settings }: { settings: SettingsState }) {
return;
}
const isTerminalElement =
target instanceof HTMLElement && !!target.closest?.(".xterm");
// Check each key binding
for (const binding of keyBindings) {
const keyStr = isMac ? binding.mac : binding.pc;
@@ -451,7 +455,10 @@ function App({ settings }: { settings: SettingsState }) {
// Don't handle them at app level
const terminalActions = ['copy', 'paste', 'selectAll', 'clearBuffer', 'searchTerminal'];
if (terminalActions.includes(binding.action)) {
return; // Let terminal handle it
if (isTerminalElement) {
return; // Let terminal handle it
}
continue; // Ignore terminal actions outside terminal
}
e.preventDefault();
@@ -464,7 +471,7 @@ function App({ settings }: { settings: SettingsState }) {
window.addEventListener('keydown', handleGlobalKeyDown, true);
return () => window.removeEventListener('keydown', handleGlobalKeyDown, true);
}, [hotkeyScheme, keyBindings, executeHotkeyAction]);
}, [hotkeyScheme, keyBindings, isHotkeyRecording, executeHotkeyAction]);
useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
+25 -1
View File
@@ -10,6 +10,7 @@ STORAGE_KEY_TERM_FONT_SIZE,
STORAGE_KEY_TERM_SETTINGS,
STORAGE_KEY_HOTKEY_SCHEME,
STORAGE_KEY_CUSTOM_KEY_BINDINGS,
STORAGE_KEY_HOTKEY_RECORDING,
STORAGE_KEY_CUSTOM_CSS,
STORAGE_KEY_UI_LANGUAGE,
} from '../../infrastructure/config/storageKeys';
@@ -99,6 +100,7 @@ export const useSettingsState = () => {
const [customKeyBindings, setCustomKeyBindings] = useState<CustomKeyBindings>(() =>
localStorageAdapter.read<CustomKeyBindings>(STORAGE_KEY_CUSTOM_KEY_BINDINGS) || {}
);
const [isHotkeyRecording, setIsHotkeyRecordingState] = useState(false);
const [customCSS, setCustomCSS] = useState<string>(() =>
localStorageAdapter.readString(STORAGE_KEY_CUSTOM_CSS) || ''
);
@@ -160,6 +162,20 @@ export const useSettingsState = () => {
if (key === STORAGE_KEY_HOTKEY_SCHEME && (value === 'disabled' || value === 'mac' || value === 'pc')) {
setHotkeyScheme(value);
}
if (key === STORAGE_KEY_CUSTOM_KEY_BINDINGS) {
if (typeof value === 'string') {
try {
setCustomKeyBindings(JSON.parse(value) as CustomKeyBindings);
} catch {
// ignore parse errors
}
} else if (value && typeof value === 'object') {
setCustomKeyBindings(value as CustomKeyBindings);
}
}
if (key === STORAGE_KEY_HOTKEY_RECORDING && typeof value === 'boolean') {
setIsHotkeyRecordingState(value);
}
});
return () => {
try {
@@ -285,7 +301,13 @@ export const useSettingsState = () => {
useEffect(() => {
localStorageAdapter.write(STORAGE_KEY_CUSTOM_KEY_BINDINGS, customKeyBindings);
}, [customKeyBindings]);
notifySettingsChanged(STORAGE_KEY_CUSTOM_KEY_BINDINGS, customKeyBindings);
}, [customKeyBindings, notifySettingsChanged]);
const setIsHotkeyRecording = useCallback((isRecording: boolean) => {
setIsHotkeyRecordingState(isRecording);
notifySettingsChanged(STORAGE_KEY_HOTKEY_RECORDING, isRecording);
}, [notifySettingsChanged]);
// Apply and persist custom CSS
useEffect(() => {
@@ -397,6 +419,8 @@ export const useSettingsState = () => {
updateKeyBinding,
resetKeyBinding,
resetAllKeyBindings,
isHotkeyRecording,
setIsHotkeyRecording,
customCSS,
setCustomCSS,
};
+9 -6
View File
@@ -17,11 +17,10 @@ import { Tabs, TabsList, TabsTrigger } from "./ui/tabs";
const isMac = typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform);
export default function SettingsPage() {
const settings = useSettingsState();
type SettingsState = ReturnType<typeof useSettingsState>;
const Inner = () => {
const { t } = useI18n();
const SettingsPageContent: React.FC<{ settings: SettingsState }> = ({ settings }) => {
const { t } = useI18n();
const { notifyRendererReady, closeSettingsWindow } = useWindowControls();
useEffect(() => {
@@ -122,6 +121,7 @@ export default function SettingsPage() {
updateKeyBinding={settings.updateKeyBinding}
resetKeyBinding={settings.resetKeyBinding}
resetAllKeyBindings={settings.resetAllKeyBindings}
setIsHotkeyRecording={settings.setIsHotkeyRecording}
/>
<SettingsSyncTab
@@ -135,11 +135,14 @@ export default function SettingsPage() {
</Tabs>
</div>
);
};
};
export default function SettingsPage() {
const settings = useSettingsState();
return (
<I18nProvider locale={settings.uiLanguage}>
<Inner />
<SettingsPageContent settings={settings} />
</I18nProvider>
);
}
+3
View File
@@ -767,6 +767,9 @@ const terminalLayerAreEqual = (prev: TerminalLayerProps, next: TerminalLayerProp
prev.terminalTheme === next.terminalTheme &&
prev.terminalSettings === next.terminalSettings &&
prev.fontSize === next.fontSize &&
prev.hotkeyScheme === next.hotkeyScheme &&
prev.keyBindings === next.keyBindings &&
prev.onHotkeyAction === next.onHotkeyAction &&
prev.onUpdateHost === next.onUpdateHost &&
prev.onToggleWorkspaceViewMode === next.onToggleWorkspaceViewMode &&
prev.onSetWorkspaceFocusedSession === next.onSetWorkspaceFocusedSession &&
@@ -14,8 +14,17 @@ export default function SettingsShortcutsTab(props: {
updateKeyBinding?: (bindingId: string, scheme: "mac" | "pc", newKey: string) => void;
resetKeyBinding?: (bindingId: string, scheme?: "mac" | "pc") => void;
resetAllKeyBindings: () => void;
setIsHotkeyRecording?: (isRecording: boolean) => void;
}) {
const { hotkeyScheme, setHotkeyScheme, keyBindings, updateKeyBinding, resetKeyBinding, resetAllKeyBindings } = props;
const {
hotkeyScheme,
setHotkeyScheme,
keyBindings,
updateKeyBinding,
resetKeyBinding,
resetAllKeyBindings,
setIsHotkeyRecording,
} = props;
const { t } = useI18n();
const [recordingBindingId, setRecordingBindingId] = useState<string | null>(null);
@@ -98,6 +107,14 @@ export default function SettingsShortcutsTab(props: {
};
}, [recordingBindingId, recordingScheme, updateKeyBinding, cancelRecording, getSpecialSuffix]);
useEffect(() => {
const isRecording = Boolean(recordingBindingId && recordingScheme);
setIsHotkeyRecording?.(isRecording);
return () => {
setIsHotkeyRecording?.(false);
};
}, [recordingBindingId, recordingScheme, setIsHotkeyRecording]);
const categories = useMemo(() => ["tabs", "terminal", "navigation", "app"] as const, []);
return (
+1
View File
@@ -13,6 +13,7 @@ export const STORAGE_KEY_TERM_FONT_SIZE = 'netcatty_term_font_size_v1';
export const STORAGE_KEY_TERM_SETTINGS = 'netcatty_term_settings_v1';
export const STORAGE_KEY_HOTKEY_SCHEME = 'netcatty_hotkey_scheme_v1';
export const STORAGE_KEY_CUSTOM_KEY_BINDINGS = 'netcatty_custom_key_bindings_v1';
export const STORAGE_KEY_HOTKEY_RECORDING = 'netcatty_hotkey_recording_v1';
export const STORAGE_KEY_CUSTOM_CSS = 'netcatty_custom_css_v1';
export const STORAGE_KEY_UI_LANGUAGE = 'netcatty_ui_language_v1';
export const STORAGE_KEY_PORT_FORWARDING = 'netcatty_port_forwarding_v1';