Copy and paste: the desktop's universal copy key works; Ctrl+C at the terminal login keeps the terminal

Omarchy's Super+C sends Ctrl+C by setting the modifier state with no Ctrl key
event while Super is held. Flutter's web engine only presses a modifier on its
own key event, so the app saw a plain "c": the chat selection was not copied
and the terminal typed the letter. A key modifier sync in front of the
framework's key handler now presses the modifiers the browser event's flags
hold (and outside Apple platforms lets go of a held Meta for Ctrl+C/V/X/A/
Insert), installed by the app's binding.

The terminal copies a selection on Ctrl+C without sending ^C, treats the
injected copy key as copy only, and pastes on Shift+Insert. A refused
clipboard write or read now shows a message instead of failing silently.

Server: before the login completes, Ctrl+C and Ctrl+\ erase the typed
password (Ctrl+U) and Ctrl+Z is dropped, so su is not killed and the terminal
no longer ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-29 17:57:28 +00:00
parent 58041e06b7
commit 3d739c3bfc
31 changed files with 2090 additions and 17 deletions
@@ -0,0 +1,28 @@
import 'key_event_modifiers_stub.dart'
if (dart.library.js_interop) 'key_event_modifiers_web.dart'
as platform;
/// The modifier flags a key event itself carries.
class KeyEventModifiers {
const KeyEventModifiers({
this.control = false,
this.shift = false,
this.alt = false,
this.meta = false,
});
final bool control;
final bool shift;
final bool alt;
final bool meta;
@override
String toString() =>
'KeyEventModifiers(control: $control, shift: $shift, alt: $alt, '
'meta: $meta)';
}
/// The flags of the browser key event being dispatched (null elsewhere, or
/// when no key event is).
KeyEventModifiers? currentKeyEventModifiers() =>
platform.currentKeyEventModifiers();
@@ -0,0 +1,3 @@
import 'key_event_modifiers.dart';
KeyEventModifiers? currentKeyEventModifiers() => null;
@@ -0,0 +1,20 @@
import 'dart:js_interop';
import 'package:web/web.dart' as web;
import 'key_event_modifiers.dart';
/// The browser's current event (`window.event`), set while its listeners
/// run, so the engine's key listener (and the key data it sends the
/// framework synchronously) is inside it.
KeyEventModifiers? currentKeyEventModifiers() {
final event = web.window.event;
if (event == null || !event.isA<web.KeyboardEvent>()) return null;
final key = event as web.KeyboardEvent;
return KeyEventModifiers(
control: key.ctrlKey,
shift: key.shiftKey,
alt: key.altKey,
meta: key.metaKey,
);
}
@@ -0,0 +1,240 @@
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'key_event_modifiers.dart';
/// Keeps the framework's modifier state ([HardwareKeyboard]) in step with the
/// modifier flags of the key event being handled.
///
/// A browser reports Ctrl, Shift, Alt and Meta on every key event, but
/// Flutter's web engine only learns that a modifier is down from that
/// modifier's own key event; it releases modifiers the flags say are up, but
/// never presses one the flags say is down. A shortcut a desktop injects
/// (Omarchy's universal copy: Super+C sends Ctrl+C by setting the modifier
/// state, with no Ctrl key event, while Super is still held) therefore
/// reached the app as a plain "c": nothing was copied, and the terminal
/// typed the letter. This sync sits in front of the framework's key data
/// handler and, for each non-modifier key, presses (synthesized) the
/// modifiers the event's flags hold that the framework does not, then
/// releases them with that key.
///
/// Outside Apple platforms Meta is never part of the clipboard keys, so a
/// Meta (Super) that is held, or merged into such a chord, does not stop
/// Ctrl+C, V, X, A or Insert from being the clipboard key: Meta is released
/// for the chord and pressed again after it.
class KeyModifierSync {
KeyModifierSync({
required this.forward,
required this.modifiers,
HardwareKeyboard? keyboard,
TargetPlatform Function()? platform,
}) : _keyboard = keyboard ?? HardwareKeyboard.instance,
_platform = platform ?? (() => defaultTargetPlatform);
/// The framework's key data handler, which this one is placed in front of.
final bool Function(ui.KeyData data) forward;
/// The flags of the key event being handled, or null when unknown.
final KeyEventModifiers? Function() modifiers;
final HardwareKeyboard _keyboard;
final TargetPlatform Function() _platform;
/// The sync installed in front of the app's key events, if any.
static KeyModifierSync? instance;
/// Modifiers pressed by this sync, not by a key.
final Set<PhysicalKeyboardKey> _added = {};
/// Held modifiers this sync released for a chord, to press again after it.
final Map<PhysicalKeyboardKey, LogicalKeyboardKey> _released = {};
/// The key whose release ends the current chord.
PhysicalKeyboardKey? _chordKey;
/// Whether Ctrl is down only because the key event says so: no Ctrl key
/// was pressed, so the chord came from a shortcut the desktop injected
/// (the "copy button"), not from the keyboard's Ctrl key.
bool get controlFromFlags => _added.contains(PhysicalKeyboardKey.controlLeft);
/// Whether Ctrl is down only because the key event says so, for the
/// installed sync (false when none is).
static bool get injectedControl => instance?.controlFromFlags ?? false;
/// Handles one key data from the engine: the framework's handler sees it
/// with the modifiers its event's flags hold.
bool handle(ui.KeyData data) {
final physical = PhysicalKeyboardKey(data.physical);
if (data.physical == 0 && data.logical == 0) return forward(data);
if (_added.contains(physical)) {
// The real key of a modifier this sync pressed: its own events win.
_release(physical, data.timeStamp);
}
if (_released.containsKey(physical)) {
// A modifier released for a chord: the framework already has it up.
// Its repeats are dropped; once it is let go (or the engine notices
// it is up) it is not pressed again after the chord.
switch (data.type) {
case ui.KeyEventType.repeat:
return false;
case ui.KeyEventType.up:
_released.remove(physical);
return false;
case ui.KeyEventType.down:
_released.remove(physical);
}
}
if (data.synthesized || _modifierKeys.contains(physical)) {
return forward(data);
}
final flags = modifiers();
if (flags != null) _reconcile(flags, data);
final handled = forward(data);
if (data.type == ui.KeyEventType.up && physical == _chordKey) {
_endChord(data.timeStamp);
}
return handled;
}
void _reconcile(KeyEventModifiers flags, ui.KeyData data) {
final logical = LogicalKeyboardKey(data.logical);
final clipboard =
flags.control &&
_clipboardKeys.contains(logical) &&
_platform() != TargetPlatform.macOS &&
_platform() != TargetPlatform.iOS;
var changed = false;
final press = <_Modifier>[];
// Releases first, so no chord ever holds both the old and new keys.
for (final modifier in _modifiers) {
final want = modifier.flag(flags) && !(clipboard && modifier.meta);
final held = modifier.keys
.where(_keyboard.physicalKeysPressed.contains)
.toList();
if (want && held.isEmpty) {
press.add(modifier);
} else if (!want) {
for (final key in held) {
if (_added.contains(key)) {
_release(key, data.timeStamp);
changed = true;
} else if (modifier.meta && clipboard) {
// A held Super (Meta) is not part of a clipboard chord here.
final logical = _keyboard.lookUpLayout(key)!;
_released[key] = logical;
_lift(key, logical, data.timeStamp);
changed = true;
}
}
}
}
for (final modifier in press) {
_press(modifier.keys.first, modifier.logical, data.timeStamp);
_added.add(modifier.keys.first);
changed = true;
}
if (changed && data.type == ui.KeyEventType.down) {
_chordKey = PhysicalKeyboardKey(data.physical);
}
}
/// Releases what the chord pressed and presses again what it released.
void _endChord(Duration timeStamp) {
_chordKey = null;
for (final key in _added.toList()) {
_release(key, timeStamp);
}
final released = Map.of(_released);
_released.clear();
released.forEach((key, logical) => _press(key, logical, timeStamp));
}
void _release(PhysicalKeyboardKey key, Duration timeStamp) {
_added.remove(key);
final logical = _keyboard.lookUpLayout(key);
if (logical != null) _lift(key, logical, timeStamp);
}
void _press(
PhysicalKeyboardKey key,
LogicalKeyboardKey logical,
Duration timeStamp,
) => forward(_synthesized(ui.KeyEventType.down, key, logical, timeStamp));
void _lift(
PhysicalKeyboardKey key,
LogicalKeyboardKey logical,
Duration timeStamp,
) => forward(_synthesized(ui.KeyEventType.up, key, logical, timeStamp));
static ui.KeyData _synthesized(
ui.KeyEventType type,
PhysicalKeyboardKey key,
LogicalKeyboardKey logical,
Duration timeStamp,
) => ui.KeyData(
timeStamp: timeStamp,
type: type,
physical: key.usbHidUsage,
logical: logical.keyId,
character: null,
synthesized: true,
);
static final Set<LogicalKeyboardKey> _clipboardKeys = {
LogicalKeyboardKey.keyC,
LogicalKeyboardKey.keyV,
LogicalKeyboardKey.keyX,
LogicalKeyboardKey.keyA,
LogicalKeyboardKey.insert,
};
static final List<_Modifier> _modifiers = [
_Modifier((f) => f.control, LogicalKeyboardKey.controlLeft, const [
PhysicalKeyboardKey.controlLeft,
PhysicalKeyboardKey.controlRight,
]),
_Modifier((f) => f.shift, LogicalKeyboardKey.shiftLeft, const [
PhysicalKeyboardKey.shiftLeft,
PhysicalKeyboardKey.shiftRight,
]),
_Modifier((f) => f.alt, LogicalKeyboardKey.altLeft, const [
PhysicalKeyboardKey.altLeft,
PhysicalKeyboardKey.altRight,
]),
_Modifier((f) => f.meta, LogicalKeyboardKey.metaLeft, const [
PhysicalKeyboardKey.metaLeft,
PhysicalKeyboardKey.metaRight,
], meta: true),
];
static final Set<PhysicalKeyboardKey> _modifierKeys = {
for (final modifier in _modifiers) ...modifier.keys,
};
}
class _Modifier {
const _Modifier(this.flag, this.logical, this.keys, {this.meta = false});
final bool Function(KeyEventModifiers flags) flag;
final LogicalKeyboardKey logical;
final List<PhysicalKeyboardKey> keys;
final bool meta;
}
/// Places a [KeyModifierSync] in front of the app's key events on the web,
/// where the browser's event flags are the modifier truth. Native embedders
/// already derive modifier state from their events' flags.
void installKeyModifierSync() {
if (!kIsWeb || KeyModifierSync.instance != null) return;
final dispatcher = ui.PlatformDispatcher.instance;
final framework = dispatcher.onKeyData;
if (framework == null) return;
final sync = KeyModifierSync(
forward: framework,
modifiers: currentKeyEventModifiers,
);
dispatcher.onKeyData = sync.handle;
KeyModifierSync.instance = sync;
}