Skip to content

Commit 8b39291

Browse files
committed
fix: keybindings issue in terminal(nano)
1 parent ba3d542 commit 8b39291

File tree

2 files changed

+178
-21
lines changed

2 files changed

+178
-21
lines changed

src/components/terminal/terminal.js

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { WebglAddon } from "@xterm/addon-webgl";
1313
import { Terminal as Xterm } from "@xterm/xterm";
1414
import confirm from "dialogs/confirm";
1515
import fonts from "lib/fonts";
16+
import keyBindings from "lib/keyBindings";
1617
import appSettings from "lib/settings";
1718
import LigaturesAddon from "./ligatures";
1819
import { getTerminalSettings } from "./terminalDefaults";
@@ -269,6 +270,56 @@ export default class TerminalComponent {
269270
}
270271
}
271272

273+
/**
274+
* Parse app keybindings into a format usable by the keyboard handler
275+
*/
276+
parseAppKeybindings() {
277+
const parsedBindings = [];
278+
279+
Object.values(keyBindings).forEach((binding) => {
280+
if (!binding.key) return;
281+
282+
// Skip editor-only keybindings in terminal
283+
if (binding.editorOnly) return;
284+
285+
// Handle multiple key combinations separated by |
286+
const keys = binding.key.split("|");
287+
288+
keys.forEach((keyCombo) => {
289+
const parts = keyCombo.split("-");
290+
const parsed = {
291+
ctrl: false,
292+
shift: false,
293+
alt: false,
294+
meta: false,
295+
key: "",
296+
};
297+
298+
parts.forEach((part) => {
299+
const lowerPart = part.toLowerCase();
300+
if (lowerPart === "ctrl") {
301+
parsed.ctrl = true;
302+
} else if (lowerPart === "shift") {
303+
parsed.shift = true;
304+
} else if (lowerPart === "alt") {
305+
parsed.alt = true;
306+
} else if (lowerPart === "meta" || lowerPart === "cmd") {
307+
parsed.meta = true;
308+
} else {
309+
// This is the actual key
310+
parsed.key = part;
311+
}
312+
});
313+
314+
if (parsed.key) {
315+
parsedBindings.push(parsed);
316+
}
317+
});
318+
});
319+
320+
return parsedBindings;
321+
}
322+
272323
/**
273324
* Setup copy/paste keyboard handlers
274325
*/
@@ -289,34 +340,46 @@ export default class TerminalComponent {
289340
return false;
290341
}
291342

292-
// Check for Ctrl+C (terminate)
293-
if (event.ctrlKey && event.key === "C") {
294-
event.preventDefault();
295-
this.websocket?.send("\x03");
296-
return false;
297-
}
298-
299-
// For app-wide keybindings, dispatch them to the app's keyboard handler
343+
// Only intercept specific app-wide keybindings, let terminal handle the rest
300344
if (event.ctrlKey || event.altKey || event.metaKey) {
301345
// Skip modifier-only keys
302346
if (["Control", "Alt", "Meta", "Shift"].includes(event.key)) {
303347
return true;
304348
}
305-
const appEvent = new KeyboardEvent("keydown", {
306-
key: event.key,
307-
ctrlKey: event.ctrlKey,
308-
shiftKey: event.shiftKey,
309-
altKey: event.altKey,
310-
metaKey: event.metaKey,
311-
bubbles: true,
312-
cancelable: true,
313-
});
314349

315-
// Dispatch to document so it gets picked up by the app's keyboard handler
316-
document.dispatchEvent(appEvent);
350+
// Get parsed app keybindings
351+
const appKeybindings = this.parseAppKeybindings();
352+
353+
// Check if this is an app-specific keybinding
354+
const isAppKeybinding = appKeybindings.some(
355+
(binding) =>
356+
binding.ctrl === event.ctrlKey &&
357+
binding.shift === event.shiftKey &&
358+
binding.alt === event.altKey &&
359+
binding.meta === event.metaKey &&
360+
binding.key === event.key,
361+
);
317362

318-
// Return false to prevent terminal from processing this key
319-
return false;
363+
if (isAppKeybinding) {
364+
const appEvent = new KeyboardEvent("keydown", {
365+
key: event.key,
366+
ctrlKey: event.ctrlKey,
367+
shiftKey: event.shiftKey,
368+
altKey: event.altKey,
369+
metaKey: event.metaKey,
370+
bubbles: true,
371+
cancelable: true,
372+
});
373+
374+
// Dispatch to document so it gets picked up by the app's keyboard handler
375+
document.dispatchEvent(appEvent);
376+
377+
// Return false to prevent terminal from processing this key
378+
return false;
379+
}
380+
381+
// For all other modifier combinations, let the terminal handle them
382+
return true;
320383
}
321384

322385
// Return true to allow normal processing for other keys

0 commit comments

Comments
 (0)