Files
Netcatty/components/terminal/runtime/kittyKeyboardBroadcast.ts
T
GOUKI9999andClaude Opus 5.5 e3894eb850 terminal: allow Shift+Enter text fallback inside ConPTY Win32 input mode (#3464)
* terminal: allow Shift+Enter text inside ConPTY Win32 input mode

Win32 input mode (PR #3247) delivers Shift+Enter to ConPTY as a lossless
INPUT_RECORD, which CLIs reading console records natively (Codex, Grok) can
use. Node/Bun CLIs read input through libuv, which drops modifier state when
translating INPUT_RECORD to VT bytes, so Claude Code and CodeBuddy still see a
bare CR and submit instead of inserting a newline (anthropics/claude-code#92771,
reproducing on 2.1.278).

While the mode is active, Netcatty also skips its Shift+Enter text fallback and
Kitty encoding, so shiftEnterNewlineText had no effect on local Windows
sessions at all.

Add shiftEnterForceText (default false): when enabled, Shift+Enter sends the
configured text even inside Win32 input mode, and consumes the keydown so no
native record is emitted as well. Add `\e` (ESC) to the escape decoder so the
text can carry the legacy Alt+Enter encoding, which is the only sequence that
survives the ConPTY input path.

Defaults keep the #2974 behaviour untouched for Codex/Grok.

* docs: record libuv input findings and the Win32 input mode opt-out

* fix(terminal): don't swallow Shift+Enter when there is no text to send

The Shift+Enter send-text interception resolved its text after already
calling preventDefault/stopPropagation, and returned false outside the
`if (shiftEnterText)` guard. When resolveShiftEnterText() yields an empty
string (e.g. a persisted empty setting) the keydown was therefore consumed
with nothing written, and the handler never fell through to the Win32
bookkeeping or Kitty paths below, so no Enter reached the session either.
The result was indistinguishable from a dead feature.

Resolve the text first and only enter the interception block when it is
non-empty; empty text now falls through exactly as it did before the
`shiftEnterForceText` setting existed, so a normal Enter is still produced.
The opt-out semantics for the non-empty case are unchanged.

Also tighten the source-shape test so it verifies the real intent: the
interception (preventDefault through return false) must live inside the
empty-text guard, which fails against the previous code.

* fix(terminal): scope Shift+Enter force-text to Win32 mode, honor it per broadcast target

Address review on #3464:

- The force-text opt-out could override a negotiated Kitty encoding that
  preserves Shift+Enter in non-Win32 sessions. The keydown decision now
  lives in shouldClaimShiftEnterForText: under Win32 input mode it follows
  shiftEnterForceText, otherwise only the existing collapse fallback
  applies. Covered by a regression test for a non-Win32 session whose Kitty
  encoding already preserves the chord.
- Broadcast Win32 targets returned the native record without consulting
  their own shiftEnterForceText. They now send their configured text for
  Shift+Enter when opted in, whether the source forwarded a key chord or a
  native Win32 record, and drop the paired release so ConPTY never sees a
  key-up without its key-down. Targets without the opt-out keep the native
  press/release pair. Tests cover source and target settings that differ.

ShiftEnterEvent now accepts KittyKeyboardEvent's optional type/modifier
fields so broadcast events can be checked with the same helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 23:36:10 +08:00

496 lines
17 KiB
TypeScript

import {
encodeKittyCompositionText,
encodeKittyKeyEvent,
encodeLegacyKeyboardEvent,
shouldEncodeKittyCompositionText,
type KittyKeyboardEvent,
type KittyKeyboardModeState,
} from "./kittyKeyboardProtocol";
import type { TerminalSettings } from "../../../domain/models";
import {
isBareShiftEnterLineEnding,
resolveShiftEnterText,
resolveWin32ForcedShiftEnterText,
shouldSendShiftEnterText,
} from "./shiftEnterText";
export type KittyKeyboardBroadcastInput =
| {
kind: "key";
event: KittyKeyboardEvent;
fallbackToLegacy?: boolean;
urgentInterrupt?: boolean;
/**
* Pairing identity for peers whose state is keyed by correlation id
* rather than the event's physical code. Defaults to the event's code
* so ordinary key broadcasts are unchanged.
*/
keyIdentity?: string;
}
| {
kind: "win32";
data: string;
event: KittyKeyboardEvent;
fallbackToLegacy?: boolean;
urgentInterrupt?: boolean;
}
| { kind: "legacy"; data: string; keyIdentity: string; urgentInterrupt?: boolean }
| { kind: "text"; text: string };
type KittyKeyboardBroadcastDispatchOptions = {
beforeUrgentInterrupt?: () => void;
};
type KittyKeyboardBroadcastHandler = (
input: KittyKeyboardBroadcastInput,
dispatchOptions?: KittyKeyboardBroadcastDispatchOptions,
) => void;
export type ResolvedKittyKeyboardBroadcastInput = {
data: string;
kittyEncoded: boolean;
urgentInterrupt: boolean;
logicalData?: string | null;
/** Let the target xterm encode this event with its active Win32 mode. */
win32Event?: KittyKeyboardEvent;
};
export const createKittyKeyboardBroadcastForwarder = (options: {
sourceSessionId: string;
isHandlingBroadcast: () => boolean;
isBroadcastEnabled: () => boolean;
isSensitiveInput?: () => boolean;
getDispatcher: () => ((
data: string,
sourceSessionId: string,
options: {
kittyKeyboardInput: KittyKeyboardBroadcastInput;
kittyKeyboardTargetSessionIds?: string[];
},
) => string[] | void) | null | undefined;
}) => {
let lastDispatcher: ReturnType<typeof options.getDispatcher>;
return (
input: KittyKeyboardBroadcastInput,
forcePairedRelease = false,
targetSessionIds?: string[],
): { targetSessionIds: string[] } | null => {
const currentDispatcher = options.getDispatcher();
if (currentDispatcher) lastDispatcher = currentDispatcher;
const dispatcher = currentDispatcher ?? (forcePairedRelease ? lastDispatcher : undefined);
if (
options.isHandlingBroadcast() ||
(!forcePairedRelease && options.isSensitiveInput?.() === true) ||
(!forcePairedRelease && !options.isBroadcastEnabled()) ||
!dispatcher
) return null;
const deliveredSessionIds = dispatcher("", options.sourceSessionId, {
kittyKeyboardInput: input,
...(targetSessionIds ? { kittyKeyboardTargetSessionIds: targetSessionIds } : {}),
});
return { targetSessionIds: deliveredSessionIds ?? targetSessionIds ?? [] };
};
};
export const clearKittyKeyboardBroadcastPairingState = (
encodedKeys: Set<string>,
legacySuppressedKeys: Set<string>,
win32ShiftEnterTextKeys?: Set<string>,
): void => {
encodedKeys.clear();
legacySuppressedKeys.clear();
win32ShiftEnterTextKeys?.clear();
};
const SNAPSHOT_MODIFIERS = [
"AltGraph", "CapsLock", "Hyper", "KittyMeta", "NumLock",
] as const;
export const createKittyKeyboardSyntheticRelease = (
event: KittyKeyboardEvent,
remainingPresses: Iterable<KittyKeyboardEvent> = [],
lockState?: { capsLock: boolean; numLock: boolean },
): KittyKeyboardEvent => {
const remaining = Array.from(remainingPresses);
const hasRemainingKey = (keys: string[], codePrefix?: string) => remaining.some((press) => (
keys.includes(press.key) || (codePrefix ? press.code?.startsWith(codePrefix) === true : false)
));
const releasedShift = event.key === "Shift" || event.code?.startsWith("Shift") === true;
const releasedControl = event.key === "Control" || event.code?.startsWith("Control") === true;
const releasedAlt = event.key === "Alt" || event.code?.startsWith("Alt") === true;
const releasedAltGraph = event.key === "AltGraph";
const releasedMeta = ["Meta", "Super"].includes(event.key) || event.code?.startsWith("Meta") === true;
const remainingShift = hasRemainingKey(["Shift"], "Shift");
const remainingControl = hasRemainingKey(["Control"], "Control");
const remainingAlt = hasRemainingKey(["Alt"], "Alt");
const remainingMeta = hasRemainingKey(["Meta", "Super"], "Meta");
const remainingAltGraph = hasRemainingKey(["AltGraph"]);
const remainingHyper = hasRemainingKey(["Hyper"]);
const remainingKittyMeta = hasRemainingKey(["KittyMeta"]);
const modifierStates = new Map<string, boolean>(
SNAPSHOT_MODIFIERS.map((name) => [name, event.getModifierState?.(name) === true]),
);
const releasedKey = event.key;
return {
...event,
type: "keyup",
repeat: false,
shiftKey: releasedShift ? remainingShift : (event.shiftKey || remainingShift),
altKey: releasedAlt || releasedAltGraph ? remainingAlt : (event.altKey || remainingAlt),
ctrlKey: releasedControl || releasedAltGraph
? remainingControl
: (event.ctrlKey || remainingControl),
metaKey: releasedMeta ? remainingMeta : (event.metaKey || remainingMeta),
getModifierState: (name) => {
if (name === "CapsLock" && lockState) return lockState.capsLock;
if (name === "NumLock" && lockState) return lockState.numLock;
if (name === "Hyper") {
return releasedKey === "Hyper" ? remainingHyper : (
modifierStates.get(name) === true || remainingHyper
);
}
if (name === "KittyMeta") {
return releasedKey === "KittyMeta" ? remainingKittyMeta : (
modifierStates.get(name) === true || remainingKittyMeta
);
}
if (name === "AltGraph") {
return releasedAltGraph ? remainingAltGraph : (
modifierStates.get(name) === true || remainingAltGraph
);
}
return modifierStates.get(name) === true;
},
};
};
export type KittyKeyboardForwardedPress = {
event: KittyKeyboardEvent;
targetSessionIds: string[];
};
export const upsertKittyKeyboardForwardedPress = (
releases: Map<string, KittyKeyboardForwardedPress>,
identity: string,
event: KittyKeyboardEvent,
targetSessionIds: string[],
): void => {
const existing = releases.get(identity);
releases.set(identity, {
event,
targetSessionIds: Array.from(new Set([
...(existing?.targetSessionIds ?? []),
...targetSessionIds,
])),
});
};
export const flushKittyKeyboardBroadcastReleases = (
releases: Map<string, KittyKeyboardForwardedPress>,
forward: (
input: KittyKeyboardBroadcastInput,
forcePairedRelease?: boolean,
targetSessionIds?: string[],
) => unknown,
currentLockState?: { capsLock: boolean; numLock: boolean },
): void => {
const pending = new Map(releases);
const entries = Array.from(pending.entries()).reverse();
const latestPress = entries[0]?.[1].event;
const lockState = currentLockState ?? {
capsLock: latestPress?.getModifierState?.("CapsLock") === true,
numLock: latestPress?.getModifierState?.("NumLock") === true,
};
for (const [identity, forwardedPress] of entries) {
pending.delete(identity);
forward({
kind: "key",
// Keep the identity the press was recorded under so a peer whose
// pairing diverges from the event's physical code (e.g. a Command+Period interrupt
// normalized to Ctrl+C) still pairs this synthetic release (#3409).
keyIdentity: identity,
event: createKittyKeyboardSyntheticRelease(
forwardedPress.event,
Array.from(pending.values(), (pendingPress) => pendingPress.event),
lockState,
),
}, true, forwardedPress.targetSessionIds);
}
releases.clear();
};
type ResolveKittyKeyboardBroadcastOptions = {
kittyProtocolEnabled: boolean;
kittyMode: KittyKeyboardModeState;
applicationCursorMode: boolean;
encodedKeys: Set<string>;
legacySuppressedKeys?: Set<string>;
win32InputMode?: boolean;
shiftEnterSettings?: Pick<
TerminalSettings,
"shiftEnterNewlineEnabled" | "shiftEnterNewlineText" | "shiftEnterForceText"
>;
/**
* Presses this Win32 target turned into its configured Shift+Enter text
* instead of a native record. Their paired releases are dropped so ConPTY
* never sees a native key-up without the matching key-down.
*/
win32ShiftEnterTextKeys?: Set<string>;
};
export const resolveWin32InputLogicalData = (
event: KittyKeyboardEvent,
applicationCursorMode: boolean,
): string | null => {
if (event.type === "keyup") return null;
if (
event.key === "Enter" &&
(event.shiftKey || event.altKey || event.ctrlKey || event.metaKey)
) {
// The native record carries semantics that a legacy CR cannot express.
// Treating a modified Enter as CR would make Netcatty record a command
// submission even when the TUI only inserted a line break.
return null;
}
return encodeLegacyKeyboardEvent(event, applicationCursorMode);
};
const resolveShiftEnterBroadcastPayload = (
input: Extract<KittyKeyboardBroadcastInput, { kind: "key" }>,
options: ResolveKittyKeyboardBroadcastOptions,
candidate: string | null,
): ResolvedKittyKeyboardBroadcastInput | null => {
if (
!shouldSendShiftEnterText(input.event, options.shiftEnterSettings) ||
candidate === null ||
!isBareShiftEnterLineEnding(candidate)
) {
return null;
}
const data = resolveShiftEnterText(options.shiftEnterSettings);
if (!data) return null;
return {
data,
kittyEncoded: false,
urgentInterrupt: false,
};
};
/**
* On a Win32 target, decide Shift+Enter from the target's own
* shiftEnterForceText setting rather than the source's, so each peer gets the
* input its own TUI can read. Returns undefined when the native record should
* be kept.
*/
const resolveWin32ForcedShiftEnterBroadcast = (
event: KittyKeyboardEvent,
identity: string,
options: ResolveKittyKeyboardBroadcastOptions,
): ResolvedKittyKeyboardBroadcastInput | null | undefined => {
if (event.type === "keyup") {
return options.win32ShiftEnterTextKeys?.delete(identity) ? null : undefined;
}
const data = resolveWin32ForcedShiftEnterText(event, options.shiftEnterSettings);
if (!data) return undefined;
options.encodedKeys.add(identity);
(options.legacySuppressedKeys ?? options.encodedKeys).add(identity);
options.win32ShiftEnterTextKeys?.add(identity);
return { data, kittyEncoded: false, urgentInterrupt: false };
};
export const resolveKittyKeyboardBroadcastInput = (
input: KittyKeyboardBroadcastInput,
options: ResolveKittyKeyboardBroadcastOptions,
): ResolvedKittyKeyboardBroadcastInput | null => {
if (input.kind === "text") {
if (options.kittyProtocolEnabled && shouldEncodeKittyCompositionText(options.kittyMode)) {
const encoded = encodeKittyCompositionText(options.kittyMode, input.text);
if (encoded) return { data: encoded, kittyEncoded: true, urgentInterrupt: false };
}
return { data: input.text, kittyEncoded: false, urgentInterrupt: false };
}
if (input.kind === "legacy") {
if ((options.legacySuppressedKeys ?? options.encodedKeys).delete(input.keyIdentity)) return null;
options.encodedKeys.add(input.keyIdentity);
return {
data: input.data,
kittyEncoded: false,
urgentInterrupt: input.urgentInterrupt === true,
};
}
if (input.kind === "win32") {
if (options.win32InputMode) {
const identity = input.event.code || input.event.key;
const legacySuppressedKeys = options.legacySuppressedKeys ?? options.encodedKeys;
if (input.event.type === "keyup") {
const hasPairedKeyDown = options.encodedKeys.delete(identity);
legacySuppressedKeys.delete(identity);
if (!hasPairedKeyDown) return null;
const forced = resolveWin32ForcedShiftEnterBroadcast(input.event, identity, options);
if (forced !== undefined) return forced;
} else {
const forced = resolveWin32ForcedShiftEnterBroadcast(input.event, identity, options);
if (forced !== undefined) return forced;
options.encodedKeys.add(identity);
legacySuppressedKeys.add(identity);
}
return {
data: input.data,
kittyEncoded: false,
urgentInterrupt: input.urgentInterrupt === true,
logicalData: resolveWin32InputLogicalData(
input.event,
options.applicationCursorMode,
),
};
}
return resolveKittyKeyboardBroadcastInput({
kind: "key",
event: input.event,
fallbackToLegacy: input.fallbackToLegacy,
urgentInterrupt: input.urgentInterrupt,
}, options);
}
const identity = input.keyIdentity ?? (input.event.code || input.event.key);
const legacySuppressedKeys = options.legacySuppressedKeys ?? options.encodedKeys;
const hasPairedKeyDown = input.event.type === "keyup"
? options.encodedKeys.delete(identity)
: false;
if (input.event.type === "keyup") legacySuppressedKeys.delete(identity);
if (input.event.type === "keyup" && !hasPairedKeyDown) return null;
if (options.win32InputMode) {
const forced = resolveWin32ForcedShiftEnterBroadcast(input.event, identity, options);
if (forced !== undefined) return forced;
if (input.event.type !== "keyup") {
options.encodedKeys.add(identity);
legacySuppressedKeys.add(identity);
}
return {
data: "",
kittyEncoded: false,
urgentInterrupt: false,
logicalData: resolveWin32InputLogicalData(
input.event,
options.applicationCursorMode,
),
win32Event: input.event,
};
}
const encoded = options.kittyProtocolEnabled
? encodeKittyKeyEvent(options.kittyMode, {
...input.event,
applicationCursorMode: options.applicationCursorMode,
})
: null;
if (encoded) {
if (input.event.type === "keyup") options.encodedKeys.delete(identity);
else {
options.encodedKeys.add(identity);
legacySuppressedKeys.add(identity);
}
const remapped = resolveShiftEnterBroadcastPayload(input, options, encoded);
if (remapped) return remapped;
return { data: encoded, kittyEncoded: true, urgentInterrupt: false };
}
if (!input.fallbackToLegacy || input.event.type === "keyup") return null;
const legacy = encodeLegacyKeyboardEvent(input.event, options.applicationCursorMode);
if (!legacy) return null;
options.encodedKeys.add(identity);
legacySuppressedKeys.add(identity);
const remapped = resolveShiftEnterBroadcastPayload(input, options, legacy);
if (remapped) return remapped;
return {
data: legacy,
kittyEncoded: false,
urgentInterrupt: input.urgentInterrupt === true,
};
};
export const createKittyKeyboardBroadcastHandler = (options: {
resolveOptions: () => ResolveKittyKeyboardBroadcastOptions;
getSessionId: () => string | null;
isSensitiveInput?: () => boolean;
isConnected: () => boolean;
isRuntimeDisposed: () => boolean;
interruptSession?: (sessionId: string) => void;
writeDisposed: (sessionId: string, data: string) => void;
writeActive: (data: string, logicalData?: string | null) => void;
writeWin32Event?: (event: KittyKeyboardEvent, logicalData: string | null) => void;
}): KittyKeyboardBroadcastHandler => (input, dispatchOptions) => {
const sessionId = options.getSessionId();
if (!sessionId || !options.isConnected()) return;
const isPairedRelease =
(input.kind === "key" || input.kind === "win32") &&
input.event.type === "keyup";
if (!isPairedRelease && options.isSensitiveInput?.() === true) return;
const resolved = resolveKittyKeyboardBroadcastInput(input, options.resolveOptions());
if (!resolved) return;
if (resolved.urgentInterrupt && options.interruptSession) {
dispatchOptions?.beforeUrgentInterrupt?.();
options.interruptSession(sessionId);
return;
}
if (resolved.win32Event) {
if (!options.isRuntimeDisposed()) {
options.writeWin32Event?.(
resolved.win32Event,
resolved.logicalData ?? null,
);
}
return;
}
if (options.isRuntimeDisposed()) {
options.writeDisposed(sessionId, resolved.data);
return;
}
options.writeActive(resolved.data, resolved.logicalData);
};
const handlers = new Map<string, KittyKeyboardBroadcastHandler>();
const pendingInputs = new Map<string, Array<{
input: KittyKeyboardBroadcastInput;
dispatchOptions?: KittyKeyboardBroadcastDispatchOptions;
}>>();
export const registerKittyKeyboardBroadcastHandler = (
sessionId: string,
handler: KittyKeyboardBroadcastHandler,
): (() => void) => {
handlers.set(sessionId, handler);
const pending = pendingInputs.get(sessionId);
if (pending) {
pendingInputs.delete(sessionId);
for (const pendingInput of pending) {
handler(pendingInput.input, pendingInput.dispatchOptions);
}
}
return () => {
if (handlers.get(sessionId) === handler) handlers.delete(sessionId);
};
};
export const clearKittyKeyboardBroadcastSession = (sessionId: string): void => {
handlers.delete(sessionId);
pendingInputs.delete(sessionId);
};
export const dispatchKittyKeyboardBroadcastInput = (
sessionId: string,
input: KittyKeyboardBroadcastInput,
dispatchOptions?: KittyKeyboardBroadcastDispatchOptions,
): boolean => {
const handler = handlers.get(sessionId);
if (!handler) {
const pending = pendingInputs.get(sessionId) ?? [];
pending.push({ input, dispatchOptions });
if (pending.length > 128) pending.shift();
pendingInputs.set(sessionId, pending);
return true;
}
handler(input, dispatchOptions);
return true;
};