(key: string | number)
| 136 | } |
| 137 | |
| 138 | function keyNameToCode(key: string | number): number { |
| 139 | if (typeof key === "number") return key; |
| 140 | const normalized = key.trim().toLowerCase(); |
| 141 | if (normalized.length === 1) return charToKeyCode(normalized) ?? 0; |
| 142 | const functionKey = /^f(\d{1,2})$/u.exec(normalized); |
| 143 | if (functionKey) { |
| 144 | const functionKeyCodes = [ |
| 145 | ZR_KEY_F1, |
| 146 | ZR_KEY_F2, |
| 147 | ZR_KEY_F3, |
| 148 | ZR_KEY_F4, |
| 149 | ZR_KEY_F5, |
| 150 | ZR_KEY_F6, |
| 151 | ZR_KEY_F7, |
| 152 | ZR_KEY_F8, |
| 153 | ZR_KEY_F9, |
| 154 | ZR_KEY_F10, |
| 155 | ZR_KEY_F11, |
| 156 | ZR_KEY_F12, |
| 157 | ] as const; |
| 158 | const index = Number(functionKey[1]) - 1; |
| 159 | const code = functionKeyCodes[index]; |
| 160 | if (code !== undefined) return code; |
| 161 | } |
| 162 | switch (normalized) { |
| 163 | case "enter": |
| 164 | case "return": |
| 165 | return ZR_KEY_ENTER; |
| 166 | case "escape": |
| 167 | case "esc": |
| 168 | return ZR_KEY_ESCAPE; |
| 169 | case "tab": |
| 170 | return ZR_KEY_TAB; |
| 171 | case "space": |
| 172 | return ZR_KEY_SPACE; |
| 173 | case "backspace": |
| 174 | return ZR_KEY_BACKSPACE; |
| 175 | case "delete": |
| 176 | case "del": |
| 177 | return ZR_KEY_DELETE; |
| 178 | case "home": |
| 179 | return ZR_KEY_HOME; |
| 180 | case "end": |
| 181 | return ZR_KEY_END; |
| 182 | case "pageup": |
| 183 | return ZR_KEY_PAGE_UP; |
| 184 | case "pagedown": |
| 185 | return ZR_KEY_PAGE_DOWN; |
| 186 | case "left": |
| 187 | return ZR_KEY_LEFT; |
| 188 | case "right": |
| 189 | return ZR_KEY_RIGHT; |
| 190 | case "up": |
| 191 | return ZR_KEY_UP; |
| 192 | case "down": |
| 193 | return ZR_KEY_DOWN; |
| 194 | default: |
| 195 | throw new Error(`Unsupported semantic scenario key ${JSON.stringify(key)}`); |
no test coverage detected