Improves hotkey handling between app and terminal

Refines detection of terminal and input contexts to ensure global hotkeys are only processed at the appropriate level, preventing conflicts between app-level and terminal-level shortcuts. Adds enhanced logging for easier debugging during development. Enables more reliable hotkey behavior and a smoother user experience.
This commit is contained in:
bincxz committed 2025-12-19 07:21:42 +08:00
1 parent 9d4d349329
commit 00481a20ee
2 files changed
+48 -5

No files matched your search

+33 -2
View File
@@ -7,6 +7,7 @@ import { useVaultState } from './application/state/useVaultState';
import { useWindowControls } from './application/state/useWindowControls';
import { I18nProvider, useI18n } from './application/i18n/I18nProvider';
import { matchesKeyBinding } from './domain/models';
import { getAppLevelActions } from './application/state/useGlobalHotkeys';
import { resolveHostAuth } from './domain/sshAuth';
import { netcattyBridge } from './infrastructure/services/netcattyBridge';
import { TopTabs } from './components/TopTabs';
@@ -438,13 +439,30 @@ function App({ settings }: { settings: SettingsState }) {
// Note: xterm terminal handles its own key interception via attachCustomKeyEventHandler
const target = e.target as HTMLElement;
const isFormElement = target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable;
const isXtermInput =
target instanceof HTMLElement &&
!!target.closest?.(".xterm, .xterm-helper-textarea, .xterm-screen, .xterm-viewport");
if (isFormElement && e.key !== 'Escape') {
if (isFormElement && !isXtermInput && e.key !== 'Escape') {
return;
}
const isTerminalElement =
target instanceof HTMLElement && !!target.closest?.(".xterm");
target instanceof HTMLElement &&
!!target.closest?.(".xterm, .xterm-helper-textarea, .xterm-screen, .xterm-viewport");
const isTerminalInPath = Boolean(
e.composedPath?.().some(
(node) =>
node instanceof HTMLElement &&
(node.classList.contains("xterm") ||
node.classList.contains("xterm-helper-textarea") ||
node.classList.contains("xterm-screen") ||
node.classList.contains("xterm-viewport") ||
node.hasAttribute("data-session-id")),
),
);
const appLevelActions = getAppLevelActions();
// Check each key binding
for (const binding of keyBindings) {
@@ -463,6 +481,19 @@ function App({ settings }: { settings: SettingsState }) {
e.preventDefault();
e.stopPropagation();
if (IS_DEV) {
console.log('[Hotkeys] Global handle', {
action: binding.action,
key: e.key,
meta: e.metaKey,
ctrl: e.ctrlKey,
alt: e.altKey,
shift: e.shiftKey,
targetTag: target?.tagName,
isTerminalElement,
isTerminalInPath,
});
}
executeHotkeyAction(binding.action, e);
return;
}
@@ -296,6 +296,9 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime
const terminalActions = getTerminalPassthroughActions();
term.attachCustomKeyEventHandler((e: KeyboardEvent) => {
if (e.type !== "keydown") {
return true;
}
if ((e.ctrlKey || e.metaKey) && e.key === "f" && e.type === "keydown") {
e.preventDefault();
ctx.setIsSearchOpen(true);
@@ -317,13 +320,22 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime
const { action } = matched;
if (appLevelActions.has(action)) {
e.preventDefault();
hotkeyCallback?.(action, e);
return false;
return true; // Let app-level handler process it
}
if (terminalActions.has(action)) {
e.preventDefault();
e.stopPropagation();
if (import.meta.env.DEV) {
console.log('[Hotkeys] Xterm terminal-level', {
action,
key: e.key,
meta: e.metaKey,
ctrl: e.ctrlKey,
alt: e.altKey,
shift: e.shiftKey,
});
}
switch (action) {
case "copy": {
const selection = term.getSelection();