Repository navigation
Fix Unix key input: no echo, full arrow keys, case-insensitive keys - #12
Merged
Merged
Conversation
readKey() now turns off ECHO and reads an arrow key's escape sequence while still in raw mode, so one arrow press is one key and macOS no longer echoes "[C". Keys are compared as ints and lowercased at setup, so upper-case and arrow keys match. A closed stdin exits instead of looping forever.
Owner
|
You have merged this code already in #13 Will keep it open, but you can close it at any time :) Cheers mate |
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.
Problem
readKey()turned offICANONbut notECHO, so every key press was printed (seen on macOS).ESC [ C), butreadKey()read 1. InsetupKeys(), pressing → set RIGHT=ESC, UP=[, LEFT=C. During play, the leftover bytes were read as extra moves.movewas a lowercased signedchar, while the stored keys wereunsigned charand not lowercased.read()returned 0, and the main loop spun forever.Fix
readKey()turns offECHO, reads the rest of an arrow sequence while still in raw mode, and returns -1 at end of input:setupKeys()stores letters lowercased:Main loop:
moveis now anintcompared againstkeys[]directly. Theunsigned charcopies are kept only for printing the hint line. This also lets Windows arrow codes (224<<8 | x) match.This also clears the
-Wconversionand-Wmissing-field-initializerswarnings from the old code.Testing
Tested on Linux in a pseudo-terminal:
I) works when pressed asiNot tested on macOS or Windows. The Windows
readKey()is unchanged.Known leftover
When arrow keys are used, the "Move (…)" hint line still prints junk characters, because it prints the keys as single characters. That's left for a follow-up.
Fixes #5