Copy a terminal selection with Ctrl+C outside Apple platforms

A browser keeps Ctrl+Shift+C for its developer tools, so on Linux and
Windows web nothing copied. With text selected Ctrl+C now copies it (and
clears the selection) instead of interrupting the process; Ctrl+Insert
copies too, and a finished run's read-only output answers the copy keys.
The terminal's options menu names the key.
This commit is contained in:
2026-11-11 00:05:16 +00:00
parent e9fa1b6ec9
commit 2628f6c425
3 changed files with 170 additions and 2 deletions
@@ -19,8 +19,10 @@ bool chatShellTouchFirst() =>
/// The terminal's own key shortcuts. Cmd-C, Cmd-V and Cmd-A on Apple
/// platforms; elsewhere Ctrl-V (and the terminal convention Ctrl-Shift-V)
/// paste and Ctrl-Shift-C copies, so every other Ctrl key, Ctrl-A and Ctrl-C
/// included, reaches the process.
/// paste and Ctrl-Shift-C or Ctrl-Insert copies, so every other Ctrl key,
/// Ctrl-A included, reaches the process. Ctrl-C copies too while text is
/// selected (see [chatShellCopyKey]); a browser keeps Ctrl-Shift-C for its
/// developer tools, so there it is the key that works.
Map<ShortcutActivator, Intent> chatShellTerminalShortcuts() {
const paste = PasteTextIntent(SelectionChangedCause.keyboard);
final apple =
@@ -39,11 +41,30 @@ Map<ShortcutActivator, Intent> chatShellTerminalShortcuts() {
return const {
SingleActivator(LogicalKeyboardKey.keyC, control: true, shift: true):
CopySelectionTextIntent.copy,
SingleActivator(LogicalKeyboardKey.insert, control: true):
CopySelectionTextIntent.copy,
SingleActivator(LogicalKeyboardKey.keyV, control: true): paste,
SingleActivator(LogicalKeyboardKey.keyV, control: true, shift: true): paste,
};
}
/// Whether [event] is Ctrl-C outside Apple platforms: with text selected in
/// the terminal it copies the selection instead of interrupting the process,
/// as terminals in browsers and on Windows do. Without a selection Ctrl-C
/// stays the process's.
bool chatShellCopyKey(KeyEvent event, HardwareKeyboard keyboard) {
final apple =
defaultTargetPlatform == TargetPlatform.macOS ||
defaultTargetPlatform == TargetPlatform.iOS;
return !apple &&
event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.keyC &&
keyboard.isControlPressed &&
!keyboard.isShiftPressed &&
!keyboard.isAltPressed &&
!keyboard.isMetaPressed;
}
/// One run's terminal emulator view. In a card it is bounded to
/// [kChatShellOutputCollapsedLines] rows (a finished run shrinks to its
/// output); with [fill] it takes the space its parent gives (full screen).
@@ -74,6 +95,7 @@ class ChatShellTerminalView extends StatefulWidget {
class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
final TerminalController _controller = TerminalController();
final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal');
final FocusNode _outputFocus = FocusNode(debugLabel: 'chat-shell-output');
@override
void initState() {
@@ -88,9 +110,42 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
ChatComposerPasteFocus.unregisterInput(_focus);
_controller.dispose();
_focus.dispose();
_outputFocus.dispose();
super.dispose();
}
/// Copies the selected text and clears the selection; false with none.
bool _copySelection() {
final selection = _controller.selection;
if (selection == null) return false;
final text = widget.session.terminal.buffer.getText(selection);
if (text.isEmpty) return false;
Clipboard.setData(ClipboardData(text: text));
_controller.clearSelection();
return true;
}
/// Ctrl-C with a selection copies; anything else is the terminal's.
KeyEventResult _key(FocusNode node, KeyEvent event) =>
chatShellCopyKey(event, HardwareKeyboard.instance) && _copySelection()
? KeyEventResult.handled
: KeyEventResult.ignored;
/// A finished run's output takes no keys of its own, so the copy keys are
/// answered here while it has the keyboard.
KeyEventResult _readOnlyKey(FocusNode node, KeyEvent event) {
if (event is! KeyDownEvent ||
event.logicalKey != LogicalKeyboardKey.keyC &&
event.logicalKey != LogicalKeyboardKey.insert) {
return KeyEventResult.ignored;
}
final keyboard = HardwareKeyboard.instance;
if (!keyboard.isControlPressed && !keyboard.isMetaPressed) {
return KeyEventResult.ignored;
}
return _copySelection() ? KeyEventResult.handled : KeyEventResult.ignored;
}
void _tapped(TapUpDetails details, CellOffset offset) {
final uri = chatShellLinkAt(widget.session.terminal.buffer, offset);
if (uri != null) openChatLink(context, uri);
@@ -127,9 +182,22 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
shortcuts: chatShellTerminalShortcuts(),
cursorType: TerminalCursorType.block,
onTapUp: _tapped,
onKeyEvent: _key,
),
),
);
if (!live) {
// The read-only emulator attaches no keyboard: a press selects text
// and gives this node the keyboard, so the copy keys work.
view = Listener(
onPointerDown: (_) => _outputFocus.requestFocus(),
child: Focus(
focusNode: _outputFocus,
onKeyEvent: _readOnlyKey,
child: view,
),
);
}
// The emulator would otherwise inset itself by the window's safe area.
view = MediaQuery.removePadding(
context: context,