MCPcopy Create free account
hub / github.com/RtlZeroMemory/Rezi / keyToBytes

Function keyToBytes

packages/node/src/testing/ptyScenario.ts:186–278  ·  view source on GitHub ↗
(key: string | number, mods: readonly string[] | undefined)

Source from the content-addressed store, hash-verified

184}
185
186function keyToBytes(key: string | number, mods: readonly string[] | undefined): string {
187 const normalized = normalizeKeyForPty(key);
188 const ctrl = mods?.includes("ctrl") ?? false;
189 const alt = mods?.includes("alt") ?? false;
190 const meta = mods?.includes("meta") ?? false;
191 const shift = mods?.includes("shift") ?? false;
192 if (
193 typeof normalized === "string" &&
194 /^f(?:[1-9]|1[0-2])$/u.test(normalized) &&
195 (ctrl || alt || meta || shift)
196 ) {
197 throw new Error(
198 `Unsupported PTY scenario key ${JSON.stringify(key)} with function-key modifiers ${JSON.stringify(mods ?? [])}`,
199 );
200 }
201 if ((alt || meta) && typeof normalized === "string" && normalized.length === 1 && !ctrl) {
202 return `\u001b${shift ? shiftPrintableChar(normalized) : normalized}`;
203 }
204 if (ctrl && typeof normalized === "string" && normalized.length === 1) {
205 const upper = normalized.toUpperCase();
206 const code = upper.charCodeAt(0);
207 if (code >= 65 && code <= 90) {
208 return String.fromCharCode(code - 64);
209 }
210 }
211 if (shift && normalized === "tab" && !ctrl && !alt && !meta) {
212 return "\u001b[Z";
213 }
214 if (typeof normalized === "string" && normalized.length === 1 && !ctrl && !alt && !meta) {
215 return shift ? shiftPrintableChar(normalized) : normalized;
216 }
217 switch (normalized) {
218 case "enter":
219 case "return":
220 return "\r";
221 case "escape":
222 case "esc":
223 return "\u001b";
224 case "tab":
225 return "\t";
226 case "space":
227 return " ";
228 case "backspace":
229 return "\u007f";
230 case "up":
231 return "\u001b[A";
232 case "down":
233 return "\u001b[B";
234 case "right":
235 return "\u001b[C";
236 case "left":
237 return "\u001b[D";
238 case "home":
239 return "\u001b[H";
240 case "end":
241 return "\u001b[F";
242 case "delete":
243 case "del":

Callers 1

eventToInputStepFunction · 0.85

Calls 2

normalizeKeyForPtyFunction · 0.85
shiftPrintableCharFunction · 0.85

Tested by

no test coverage detected