Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console.ReadKey] Return the correct KeyChar on Alt+Symbol chords #39539

Closed

Conversation

eiriktsarpalis
Copy link
Member

@eiriktsarpalis eiriktsarpalis commented Jul 17, 2020

Contributes to #802.

Attempts to address an issue on Unix where using Console.ReadKey() to read Alt + symbol chords will return the Escape character. For example:

$ dotnet fsi

> System.Console.ReadKey() ;; // Alt + #
val it : System.ConsoleKeyInfo = System.ConsoleKeyInfo {Key = Escape;
                                                        KeyChar = '\027';
                                                        Modifiers = 0;}

The current behaviour is by design, escape is returned whenever the trailing character cannot be conclusively mapped to a keychord. However this often results in loss of information in otherwise legal key chords.

This PR adds a conservative change, where if the trailing character is an ASCII symbol, then that character will be returned instead of Escape:

> System.Console.ReadKey() ;; // Alt + #
#val it : System.ConsoleKeyInfo = System.ConsoleKeyInfo {Key = 0;
                                                        KeyChar = '#';
                                                        Modifiers = Alt;}

Note that this change will still drop valid keychords (e.g. Alt + £ in UK keyboard layouts).

Should be merged after #39460.

@ghost
Copy link

ghost commented Jul 17, 2020

Tagging subscribers to this area: @eiriktsarpalis
Notify danmosemsft if you want to be subscribed.

@eiriktsarpalis
Copy link
Member Author

Could I get some feedback on this PR? It's unlikely it will make it for 5.0 but if people feel it's in the right direction I could maybe try to get it in.

private bool IsPotentialKeyCharacter(char keyChar)
{
// symbol or punctuation ascii characters
return keyChar < '\x0080' && (char.IsSymbol(keyChar) || char.IsPunctuation(keyChar));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest char.IsAscii(char) but it's private for some reason (don't know why, seems like a reasonable API). There is Rune.IsAscii(keyChar) but I think that's less readable 😐

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps something we could make public?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eiriktsarpalis eiriktsarpalis modified the milestones: 5.0.0, 6.0.0 Aug 17, 2020
@eiriktsarpalis
Copy link
Member Author

Pushing to 6.0

@tmds
Copy link
Member

tmds commented Dec 8, 2020

@eiriktsarpalis are you still working on this? or can you close the PR?

@eiriktsarpalis
Copy link
Member Author

Not actively working on this at the moment. I think we should just close.

@ghost ghost locked as resolved and limited conversation to collaborators Jan 7, 2021
@eiriktsarpalis eiriktsarpalis deleted the console-fix-alt-symbols branch June 2, 2021 20:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants