read_char: Handle non-tty terminals explicitly #124
Merged
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.
Key::Unknown
is produced only when:key_from_key_code
function under Windows:console/src/windows_term.rs
Line 337 in b3ecb7d
read_key
is invoked on a non-tty terminal:console/src/term.rs
Lines 274 to 276 in b3ecb7d
The two places in the code which explicitly handled
Key::Unknown
were treating it as a proxy for an "unattended terminal" check:console/src/term.rs
Lines 258 to 263 in b3ecb7d
Since this is only 100% valid to do when running under *nix I've replaced these handlers with an explicit
!self.is_tty
check instead, which is a common pattern in the Term code.Key::Unknown
is now assumed to represent an unprintable / control key and skipped when being evaluated.I found this issue when pressing the
Ctrl
key while using theread_key
function, which causes it to returnErr(Not a terminal)
under Windows (but works fine on Linux). In this case, I was in the process of pressingCtrl + C
in order to terminate the program.It looks like this issue also occurred previously for the
Shift
andAlt
keys under Windows - rather than extend theKeys
enum further to support theCtrl
key I think the proposed fix is a little more future proof (though you may like to add support for VK_CONTROL for the sake of completeness anyway).