Skip to content

Commit 498835f

Browse files
simyosimyo
andauthored
fix(terminal): stop Ctrl+Shift+V pasting twice (Acode-Foundation#2728)
xterm.js calls attachCustomKeyEventHandler on both keydown and keyup, but the handler didn't check event.type, so paste/copy/font-zoom ran twice per keypress. Gate the side-effecting calls to keydown only. Co-authored-by: simyo <simyo@Simyo-PC2>
1 parent 0c6676e commit 498835f

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

src/components/terminal/terminal.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,22 @@ export default class TerminalComponent {
439439
setupCopyPasteHandlers() {
440440
// Add keyboard event listener to terminal element
441441
this.terminal.attachCustomKeyEventHandler((event) => {
442+
// xterm.js invokes this handler for both "keydown" and "keyup", so
443+
// any side-effecting action must only run once, on keydown, or it
444+
// fires twice per keypress (e.g. paste happening twice).
445+
const isKeyDown = event.type === "keydown";
446+
442447
// Check for Ctrl+Shift+C (copy)
443448
if (event.ctrlKey && event.shiftKey && event.key === "C") {
444449
event.preventDefault();
445-
this.copySelection();
450+
if (isKeyDown) this.copySelection();
446451
return false;
447452
}
448453

449454
// Check for Ctrl+Shift+V (paste)
450455
if (event.ctrlKey && event.shiftKey && event.key === "V") {
451456
event.preventDefault();
452-
this.pasteFromClipboard();
457+
if (isKeyDown) this.pasteFromClipboard();
453458
return false;
454459
}
455460

@@ -462,7 +467,7 @@ export default class TerminalComponent {
462467
(event.key === "+" || event.key === "=")
463468
) {
464469
event.preventDefault();
465-
this.increaseFontSize();
470+
if (isKeyDown) this.increaseFontSize();
466471
return false;
467472
}
468473

@@ -474,7 +479,7 @@ export default class TerminalComponent {
474479
event.key === "-"
475480
) {
476481
event.preventDefault();
477-
this.decreaseFontSize();
482+
if (isKeyDown) this.decreaseFontSize();
478483
return false;
479484
}
480485

@@ -494,8 +499,13 @@ export default class TerminalComponent {
494499
binding.key === eventKey,
495500
);
496501

497-
if (binding && executeCommand(binding.name)) {
498-
return false;
502+
if (binding) {
503+
if (isKeyDown) {
504+
this._lastAppKeybindingHandled = executeCommand(binding.name);
505+
}
506+
if (this._lastAppKeybindingHandled) {
507+
return false;
508+
}
499509
}
500510
}
501511

0 commit comments

Comments
 (0)