MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / parseMultipleKeypresses

Function parseMultipleKeypresses

source code/ink/parse-keypress.ts:215–304  ·  view source on GitHub ↗
(
  prevState: KeyParseState,
  input: Buffer | string | null = '',
)

Source from the content-addressed store, hash-verified

213}
214
215export function parseMultipleKeypresses(
216 prevState: KeyParseState,
217 input: Buffer | string | null = '',
218): [ParsedInput[], KeyParseState] {
219 const isFlush = input === null
220 const inputString = isFlush ? '' : inputToString(input)
221
222 // Get or create tokenizer
223 const tokenizer = prevState._tokenizer ?? createTokenizer({ x10Mouse: true })
224
225 // Tokenize the input
226 const tokens = isFlush ? tokenizer.flush() : tokenizer.feed(inputString)
227
228 // Convert tokens to parsed keys, handling paste mode
229 const keys: ParsedInput[] = []
230 let inPaste = prevState.mode === 'IN_PASTE'
231 let pasteBuffer = prevState.pasteBuffer
232
233 for (const token of tokens) {
234 if (token.type === 'sequence') {
235 if (token.value === PASTE_START) {
236 inPaste = true
237 pasteBuffer = ''
238 } else if (token.value === PASTE_END) {
239 // Always emit a paste key, even for empty pastes. This allows
240 // downstream handlers to detect empty pastes (e.g., for clipboard
241 // image handling on macOS). The paste content may be empty string.
242 keys.push(createPasteKey(pasteBuffer))
243 inPaste = false
244 pasteBuffer = ''
245 } else if (inPaste) {
246 // Sequences inside paste are treated as literal text
247 pasteBuffer += token.value
248 } else {
249 const response = parseTerminalResponse(token.value)
250 if (response) {
251 keys.push({ kind: 'response', sequence: token.value, response })
252 } else {
253 const mouse = parseMouseEvent(token.value)
254 if (mouse) {
255 keys.push(mouse)
256 } else {
257 keys.push(parseKeypress(token.value))
258 }
259 }
260 }
261 } else if (token.type === 'text') {
262 if (inPaste) {
263 pasteBuffer += token.value
264 } else if (
265 /^\[<\d+;\d+;\d+[Mm]$/.test(token.value) ||
266 /^\[M[\x60-\x7f][\x20-\uffff]{2}$/.test(token.value)
267 ) {
268 // Orphaned SGR/X10 mouse tail (fullscreen only — mouse tracking is off
269 // otherwise). A heavy render blocked the event loop past App's 50ms
270 // flush timer, so the buffered ESC was flushed as a lone Escape and
271 // the continuation `[<btn;col;rowM` arrived as text. Re-synthesize
272 // with the ESC prefix so the scroll event still fires instead of

Callers 1

AppClass · 0.85

Calls 8

inputToStringFunction · 0.85
createTokenizerFunction · 0.85
createPasteKeyFunction · 0.85
parseTerminalResponseFunction · 0.85
parseMouseEventFunction · 0.85
parseKeypressFunction · 0.85
feedMethod · 0.80
flushMethod · 0.45

Tested by

no test coverage detected