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

Let System.ConsoleKeyInfo able to represent keypress in the Unicode world #51085

Closed
Tracked by #64487
WenceyWang opened this issue Apr 11, 2021 · 3 comments
Closed
Tracked by #64487
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Console
Milestone

Comments

@WenceyWang
Copy link

WenceyWang commented Apr 11, 2021

Background and Motivation

The design of keyboard key code and key char is dated back to IBM PC and we have code pages to make things work.

Currently, we have much more complicated keyboards, some of them can type in script languages, some of them can directly input emoji.

But System.ConsoleKeyInfo uses a char to store key char. Which caused a lot of long hanging serious issue:

#27828 is only one of the examples.

These problems can be easily fixed by adding a new property.

Proposed API

namespace System
{
  public struct ConsoleKeyInfo {
+    public ReadOnlySpan<System.Text.Rune> KeyCharacters { get; }
}

Usage Examples

StringBuilder inputText = new StringBuilder ( );

while ( true )
{
	ConsoleKeyInfo key = Console . ReadKey ( true );

	switch ( key.Key )
	{
		case ConsoleKey.Escape:
		case ConsoleKey.Enter:
			// Some Logic
			break;
		default:
			foreach ( Rune rune in key.KeyCharacters )
			{
				inputText.Append ( rune );
			}
			break;
	}
	
}

Alternative Designs

The property type can also be ReadOnlySpan<char> or even string. The name can also be different.

Risks

ConsoleKeyInfo is [Serializable].

@WenceyWang WenceyWang added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 11, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Console untriaged New issue has not been triaged by the area owner labels Apr 11, 2021
@ghost
Copy link

ghost commented Apr 11, 2021

Tagging subscribers to this area: @carlossanlop
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and Motivation

The design of keyboard key code and key char is dated back to IBM PC and we have code pages to make things work.

Currently, we have much more complicated keyboards, some of them can type in script languages, some of them can directly input emoji.

But System.ConsoleKeyInfo uses a char to store key char. Which caused a lot of long hanging serious issue:

#27828 is only one of the examples.

This problem can be easily fixed by adding a new property.

Proposed API

namespace System
{
  public struct ConsoleKeyInfo {
+    public ReadOnlySpan<System.Text.Rune> KeyCharacters { get; }
}

Usage Examples

StringBuilder inputText = new StringBuilder ( );

while ( true )
{
	ConsoleKeyInfo key = Console . ReadKey ( true );

	switch ( key.Key )
	{
		case ConsoleKey.Escape:
		case ConsoleKey.Enter:
			// Some Logic
			break;
		default:
			foreach ( Rune rune in key.KeyCharacters )
			{
				inputText.Append ( rune );
			}
			break;
	}
	
}

Alternative Designs

The property type can also be ReadOnlySpan<char> or even string. The name can also be different.

Risks

ConsoleKeyInfo is [Serializable].

Author: WenceyWang
Assignees: -
Labels:

api-suggestion, area-System.Console, untriaged

Milestone: -

@GrabYourPitchforks
Copy link
Member

For reference, there's a discussion going on at #27828 on whether a string-returning API needs to be added, or whether callers can keep querying the KeyChar property over and over in a loop, stitching together the key presses and building up the input string.

@jeffhandley jeffhandley added this to the 7.0.0 milestone Jul 13, 2021
@jeffhandley jeffhandley removed the untriaged New issue has not been triaged by the area owner label Jul 13, 2021
@jeffhandley jeffhandley modified the milestones: 7.0.0, 8.0.0, Future Jul 9, 2022
@adamsitnik
Copy link
Member

Closing as a duplicate of #27828

@adamsitnik adamsitnik closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Console
Projects
None yet
Development

No branches or pull requests

4 participants