|
| 1 | +import type { PreviewAutomationPressInput } from "@t3tools/contracts"; |
| 2 | + |
| 3 | +interface KeyDefinition { |
| 4 | + readonly code: string; |
| 5 | + readonly key: string; |
| 6 | + readonly keyCode: number; |
| 7 | + readonly text?: string; |
| 8 | + readonly location?: number; |
| 9 | + readonly shiftedKey?: string; |
| 10 | +} |
| 11 | + |
| 12 | +export interface PreviewAutomationKeyEvent { |
| 13 | + readonly [key: string]: unknown; |
| 14 | + readonly type: "keyDown" | "rawKeyDown" | "keyUp"; |
| 15 | + readonly key: string; |
| 16 | + readonly code: string; |
| 17 | + readonly modifiers: number; |
| 18 | + readonly windowsVirtualKeyCode: number; |
| 19 | + readonly location: number; |
| 20 | + readonly isKeypad: boolean; |
| 21 | + readonly text?: string; |
| 22 | + readonly unmodifiedText?: string; |
| 23 | +} |
| 24 | + |
| 25 | +export interface PreviewAutomationKeySequence { |
| 26 | + readonly keyDown: PreviewAutomationKeyEvent; |
| 27 | + readonly keyUp: PreviewAutomationKeyEvent; |
| 28 | + readonly signal: { |
| 29 | + readonly kind: "key"; |
| 30 | + readonly key: string; |
| 31 | + readonly code: string; |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +const NAMED_KEYS: Readonly<Record<string, KeyDefinition>> = { |
| 36 | + Escape: { code: "Escape", key: "Escape", keyCode: 27 }, |
| 37 | + Backspace: { code: "Backspace", key: "Backspace", keyCode: 8 }, |
| 38 | + Tab: { code: "Tab", key: "Tab", keyCode: 9 }, |
| 39 | + Enter: { code: "Enter", key: "Enter", keyCode: 13, text: "\r" }, |
| 40 | + Shift: { code: "ShiftLeft", key: "Shift", keyCode: 16, location: 1 }, |
| 41 | + Control: { code: "ControlLeft", key: "Control", keyCode: 17, location: 1 }, |
| 42 | + Alt: { code: "AltLeft", key: "Alt", keyCode: 18, location: 1 }, |
| 43 | + Meta: { code: "MetaLeft", key: "Meta", keyCode: 91, location: 1 }, |
| 44 | + CapsLock: { code: "CapsLock", key: "CapsLock", keyCode: 20 }, |
| 45 | + Space: { code: "Space", key: " ", keyCode: 32, text: " " }, |
| 46 | + PageUp: { code: "PageUp", key: "PageUp", keyCode: 33 }, |
| 47 | + PageDown: { code: "PageDown", key: "PageDown", keyCode: 34 }, |
| 48 | + End: { code: "End", key: "End", keyCode: 35 }, |
| 49 | + Home: { code: "Home", key: "Home", keyCode: 36 }, |
| 50 | + ArrowLeft: { code: "ArrowLeft", key: "ArrowLeft", keyCode: 37 }, |
| 51 | + ArrowUp: { code: "ArrowUp", key: "ArrowUp", keyCode: 38 }, |
| 52 | + ArrowRight: { code: "ArrowRight", key: "ArrowRight", keyCode: 39 }, |
| 53 | + ArrowDown: { code: "ArrowDown", key: "ArrowDown", keyCode: 40 }, |
| 54 | + Insert: { code: "Insert", key: "Insert", keyCode: 45 }, |
| 55 | + Delete: { code: "Delete", key: "Delete", keyCode: 46 }, |
| 56 | +}; |
| 57 | + |
| 58 | +const PRINTABLE_KEYS: ReadonlyArray<KeyDefinition> = [ |
| 59 | + { code: "Backquote", key: "`", shiftedKey: "~", keyCode: 192 }, |
| 60 | + { code: "Digit1", key: "1", shiftedKey: "!", keyCode: 49 }, |
| 61 | + { code: "Digit2", key: "2", shiftedKey: "@", keyCode: 50 }, |
| 62 | + { code: "Digit3", key: "3", shiftedKey: "#", keyCode: 51 }, |
| 63 | + { code: "Digit4", key: "4", shiftedKey: "$", keyCode: 52 }, |
| 64 | + { code: "Digit5", key: "5", shiftedKey: "%", keyCode: 53 }, |
| 65 | + { code: "Digit6", key: "6", shiftedKey: "^", keyCode: 54 }, |
| 66 | + { code: "Digit7", key: "7", shiftedKey: "&", keyCode: 55 }, |
| 67 | + { code: "Digit8", key: "8", shiftedKey: "*", keyCode: 56 }, |
| 68 | + { code: "Digit9", key: "9", shiftedKey: "(", keyCode: 57 }, |
| 69 | + { code: "Digit0", key: "0", shiftedKey: ")", keyCode: 48 }, |
| 70 | + { code: "Minus", key: "-", shiftedKey: "_", keyCode: 189 }, |
| 71 | + { code: "Equal", key: "=", shiftedKey: "+", keyCode: 187 }, |
| 72 | + { code: "Backslash", key: "\\", shiftedKey: "|", keyCode: 220 }, |
| 73 | + { code: "BracketLeft", key: "[", shiftedKey: "{", keyCode: 219 }, |
| 74 | + { code: "BracketRight", key: "]", shiftedKey: "}", keyCode: 221 }, |
| 75 | + { code: "Semicolon", key: ";", shiftedKey: ":", keyCode: 186 }, |
| 76 | + { code: "Quote", key: "'", shiftedKey: '"', keyCode: 222 }, |
| 77 | + { code: "Comma", key: ",", shiftedKey: "<", keyCode: 188 }, |
| 78 | + { code: "Period", key: ".", shiftedKey: ">", keyCode: 190 }, |
| 79 | + { code: "Slash", key: "/", shiftedKey: "?", keyCode: 191 }, |
| 80 | +]; |
| 81 | + |
| 82 | +const modifierMask = (modifiers: PreviewAutomationPressInput["modifiers"]): number => |
| 83 | + (modifiers ?? []).reduce((value, modifier) => { |
| 84 | + switch (modifier) { |
| 85 | + case "Alt": |
| 86 | + return value | 1; |
| 87 | + case "Control": |
| 88 | + return value | 2; |
| 89 | + case "Meta": |
| 90 | + return value | 4; |
| 91 | + case "Shift": |
| 92 | + return value | 8; |
| 93 | + } |
| 94 | + }, 0); |
| 95 | + |
| 96 | +function resolveKeyDefinition(input: PreviewAutomationPressInput): KeyDefinition { |
| 97 | + const named = NAMED_KEYS[input.key]; |
| 98 | + if (named) return named; |
| 99 | + |
| 100 | + const functionKey = /^F([1-9]|1[0-2])$/.exec(input.key); |
| 101 | + if (functionKey) { |
| 102 | + const number = Number(functionKey[1]); |
| 103 | + return { code: input.key, key: input.key, keyCode: 111 + number }; |
| 104 | + } |
| 105 | + |
| 106 | + if (/^[a-z]$/i.test(input.key)) { |
| 107 | + const upper = input.key.toUpperCase(); |
| 108 | + const shifted = input.modifiers?.includes("Shift") ?? false; |
| 109 | + const key = shifted || input.key === upper ? upper : input.key; |
| 110 | + return { code: `Key${upper}`, key, keyCode: upper.charCodeAt(0), text: key }; |
| 111 | + } |
| 112 | + |
| 113 | + const printable = PRINTABLE_KEYS.find( |
| 114 | + (definition) => definition.key === input.key || definition.shiftedKey === input.key, |
| 115 | + ); |
| 116 | + if (printable) { |
| 117 | + const shifted = input.modifiers?.includes("Shift") ?? false; |
| 118 | + const key = |
| 119 | + printable.shiftedKey && (shifted || input.key === printable.shiftedKey) |
| 120 | + ? printable.shiftedKey |
| 121 | + : printable.key; |
| 122 | + return { ...printable, key, text: key }; |
| 123 | + } |
| 124 | + |
| 125 | + return { |
| 126 | + code: input.key.length > 1 ? input.key : "", |
| 127 | + key: input.key, |
| 128 | + keyCode: 0, |
| 129 | + ...(input.key.length === 1 ? { text: input.key } : {}), |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Build Chromium CDP key packets using the same required fields and down-event |
| 135 | + * choice as Playwright's pinned Chromium keyboard implementation. |
| 136 | + */ |
| 137 | +export function makePreviewAutomationKeySequence( |
| 138 | + input: PreviewAutomationPressInput, |
| 139 | +): PreviewAutomationKeySequence { |
| 140 | + const definition = resolveKeyDefinition(input); |
| 141 | + const modifiers = modifierMask(input.modifiers); |
| 142 | + const suppressText = input.modifiers?.some((modifier) => modifier !== "Shift") ?? false; |
| 143 | + const text = suppressText ? "" : (definition.text ?? ""); |
| 144 | + const location = definition.location ?? 0; |
| 145 | + const shared = { |
| 146 | + key: definition.key, |
| 147 | + code: definition.code, |
| 148 | + modifiers, |
| 149 | + windowsVirtualKeyCode: definition.keyCode, |
| 150 | + location, |
| 151 | + isKeypad: location === 3, |
| 152 | + }; |
| 153 | + |
| 154 | + return { |
| 155 | + keyDown: { |
| 156 | + type: text ? "keyDown" : "rawKeyDown", |
| 157 | + ...shared, |
| 158 | + ...(text ? { text, unmodifiedText: text } : {}), |
| 159 | + }, |
| 160 | + keyUp: { type: "keyUp", ...shared }, |
| 161 | + signal: { kind: "key", key: definition.key, code: definition.code }, |
| 162 | + }; |
| 163 | +} |
0 commit comments