From 47eeffcfd187e436d96cae32a24285d93825ffd1 Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Sat, 14 Nov 2026 21:06:26 +0800 Subject: [PATCH] fix: copy and paste in the serial terminal Every key went to the port, so nothing could copy a selection: Ctrl+C is the device's, and in the desktop app nothing else reaches the clipboard. Ctrl+Shift+C / Ctrl+Shift+V (Cmd+C / Cmd+V on macOS) now copy the selection and paste, as in the SSH terminal. Other keys still go to the port. Fixes #2 --- src/frontend/Serial.tsx | 2 + src/frontend/clipboard-keys.ts | 39 ++++++++++++++++++ tests/frontend/clipboard-keys.test.ts | 57 +++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/frontend/clipboard-keys.ts create mode 100644 tests/frontend/clipboard-keys.test.ts diff --git a/src/frontend/Serial.tsx b/src/frontend/Serial.tsx index 8af2c78..f419696 100644 --- a/src/frontend/Serial.tsx +++ b/src/frontend/Serial.tsx @@ -8,6 +8,7 @@ import { } from "react"; import { useXTerm } from "react-xtermjs"; import { FitAddon } from "@xterm/addon-fit"; +import { clipboardKeyHandler } from "./clipboard-keys.js"; import { ConnectionScreen, type ConnectionStatus, @@ -295,6 +296,7 @@ export const Serial = forwardRef(function Serial( terminal.loadAddon(fitAddon); terminal.options.cursorBlink = true; terminal.options.scrollback = 10000; + terminal.attachCustomKeyEventHandler(clipboardKeyHandler(terminal)); const onData = terminal.onData((data) => { if (!connectedRef.current) return; diff --git a/src/frontend/clipboard-keys.ts b/src/frontend/clipboard-keys.ts new file mode 100644 index 0000000..a49509c --- /dev/null +++ b/src/frontend/clipboard-keys.ts @@ -0,0 +1,39 @@ +import type { Terminal } from "@xterm/xterm"; + +type ClipboardTerminal = Pick< + Terminal, + "hasSelection" | "getSelection" | "clearSelection" | "paste" +>; +type TextClipboard = Pick; + +/** + * Ctrl+Shift+C / Ctrl+Shift+V (Cmd+C / Cmd+V on macOS) copy and paste, as in + * the SSH terminal; every other key goes to the port. Without this nothing + * reaches the clipboard in the desktop app, since Ctrl+C is the device's. + */ +export function clipboardKeyHandler( + terminal: ClipboardTerminal, + clipboard: TextClipboard = navigator.clipboard, +): (event: KeyboardEvent) => boolean { + return (event) => { + const shortcut = + event.type === "keydown" && + !event.altKey && + (event.metaKey || (event.ctrlKey && event.shiftKey)); + if (!shortcut) return true; + if (event.code === "KeyC") { + if (terminal.hasSelection()) { + void clipboard.writeText(terminal.getSelection()); + terminal.clearSelection(); + } + return false; + } + if (event.code === "KeyV") { + void clipboard.readText().then((text) => { + if (text) terminal.paste(text); + }); + return false; + } + return true; + }; +} diff --git a/tests/frontend/clipboard-keys.test.ts b/tests/frontend/clipboard-keys.test.ts new file mode 100644 index 0000000..e684f81 --- /dev/null +++ b/tests/frontend/clipboard-keys.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, it, vi } from "vitest"; +import { clipboardKeyHandler } from "../../src/frontend/clipboard-keys.js"; + +function setup(selection = "") { + const terminal = { + hasSelection: () => selection !== "", + getSelection: () => selection, + clearSelection: vi.fn(), + paste: vi.fn(), + }; + const clipboard = { + writeText: vi.fn(async () => {}), + readText: vi.fn(async () => "pasted"), + }; + return { + terminal, + clipboard, + handle: clipboardKeyHandler(terminal, clipboard), + }; +} + +const key = (code: string, mods: Partial = {}) => + ({ + type: "keydown", + code, + ctrlKey: false, + shiftKey: false, + metaKey: false, + altKey: false, + ...mods, + }) as KeyboardEvent; + +describe("serial clipboard keys", () => { + it("copies the selection with Ctrl+Shift+C or Cmd+C", () => { + for (const mods of [{ ctrlKey: true, shiftKey: true }, { metaKey: true }]) { + const { terminal, clipboard, handle } = setup("AT+OK"); + expect(handle(key("KeyC", mods))).toBe(false); + expect(clipboard.writeText).toHaveBeenCalledWith("AT+OK"); + expect(terminal.clearSelection).toHaveBeenCalled(); + } + }); + + it("pastes with Ctrl+Shift+V", async () => { + const { terminal, handle } = setup(); + expect(handle(key("KeyV", { ctrlKey: true, shiftKey: true }))).toBe(false); + await vi.waitFor(() => + expect(terminal.paste).toHaveBeenCalledWith("pasted"), + ); + }); + + it("leaves Ctrl+C and plain keys to the device", () => { + const { clipboard, handle } = setup("AT+OK"); + expect(handle(key("KeyC", { ctrlKey: true }))).toBe(true); + expect(handle(key("KeyA"))).toBe(true); + expect(clipboard.writeText).not.toHaveBeenCalled(); + }); +});