Fix useInput crash on unmapped keyName codes#902
Merged
sindresorhus merged 2 commits intoMar 21, 2026
Merged
Conversation
parseKeypress used a non-null assertion (keyName[code]!) that returned undefined for escape sequences not in the keyName map (e.g. Ctrl+F1 sending ESC[1;5P). This undefined propagated to use-input.ts where .startsWith() was called on it, causing a TypeError crash. - Replace keyName[code]! with keyName[code] ?? '' to handle unmapped codes - Add defensive ?? '' guard in use-input.ts ctrl branch - Add missing vt220-style F1-F4 entries ([P]-[S]) to keyName map - Add unit tests for parseKeypress and integration tests for the fix Fixes vadimdemedes#901
- Use Unicode escapes (\u001B) instead of hex escapes (\x1b) - Capitalize comments to satisfy capitalized-comments rule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TypeError: Cannot read properties of undefined (reading 'startsWith')crash inuseInputwhen the terminal sends escape sequences with Ctrl modifiers whose key codes aren't in thekeyNamemap (e.g. Ctrl+F1 viaESC[1;5P)keyName[code]!non-null assertion inparse-keypress.tsreturnedundefinedfor unmapped codes, which propagated touse-input.tsand crashed on.startsWith()[P]–[S]) to thekeyNamemap so these keys resolve correctlyFixes #901
Changes
src/parse-keypress.ts:keyName[code]!withkeyName[code] ?? ''— removes unsafe non-null assertion[P→f1,[Q→f2,[R→f3,[S→f4tokeyNamemapsrc/hooks/use-input.ts:?? ''guard onkeypress.namein thectrlbranchTests:
test/parse-keypress.tsunit tests verifying key identity and empty-string fallbackTest plan
npx tsc --noEmitpasseshooks-use-input,hooks-use-input-navigation,hooks-use-input-kitty,input-parser,parse-keypress)