Skip to content

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

Closed as not planned

Description

@WenceyWang

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].

Activity

  1. added
    api-suggestion
    on Apr 11, 2021
  2. ghost added
    untriaged
    on Apr 11, 2021
  3. GrabYourPitchforks commented on Apr 13, 2021

    @GrabYourPitchforks
    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.

  4. added this to the 7.0.0 milestone on Jul 13, 2021
  5. removed
    untriaged
    on Jul 13, 2021
  6. modified the milestones: 7.0.0, 8.0.0, Future on Jul 9, 2022
  7. adamsitnik commented on Nov 13, 2023

    @adamsitnik
    Member

    Closing as a duplicate of #27828

  8. locked and limited conversation to collaborators on Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Console

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions