簡體   English   中英

如何在C#控制台應用程序的行首處允許用戶輸入?

[英]How can I allow user input from the beginning of the line in C# console app?

我知道我可以使用Console.ReadLine()來允許用戶輸入,但是它只允許從光標所在的位置進行用戶輸入,我希望有一種方法可以允許用戶從行的開頭(整行)而不僅僅是從光標所在的位置開始。 例如:

for (int i = 1;i <= 5;i++) Console.Write(i+" ");
string x = Console.ReadLine();

這將給出以下輸出:

1 2 3 4 5 _

在“ _”指的是光標的情況下,這樣,用戶將只能從光標位置進行寫操作-他也不能使用退格鍵刪除以前的字符。 因此,當光標不在行首時,如何允許用戶在整行上輸入?

這是准備使用自定義輸入處理的示例。 當然,這需要對性能進行一些調整,但是它應該使您和從哪里開始有了想法。

    static void Main(string[] args)
    {
        List<char> outputString = new List<char>();
        outputString = "test".ToCharArray().ToList();
        //If there is something in outputString write it to buffer and set cursor position
        if (outputString.Count > 0)
        {
            Console.Write(outputString.Select(x => x.ToString()).Aggregate((x, y) => x + y));
        }

        while (true)
        {
            //This will read user input but it will not print it to the console window 
            var key = Console.ReadKey(true);
            //Allow user maniputate cursor to the right
            //Here you can add another condition to prevent line overflow or some constrains
            //if(Console.CursorLeft < 30)
            if (key.Key == ConsoleKey.RightArrow && Console.CursorLeft < outputString.Count)
            {
                Console.SetCursorPosition(Console.CursorLeft + 1, Console.CursorTop);
            }
            else if (key.Key == ConsoleKey.LeftArrow && Console.CursorLeft > 0)
            {
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
            }
            //Here you should add all keys that your input should take, special characters are ok above ASCII number 32 which is space
            else if (key.KeyChar >= 48 && key.KeyChar <= 57 || //Read numbers
                key.KeyChar >= 64 && key.KeyChar <= 90 || //Read capital letters
                key.KeyChar >= 97 && key.KeyChar <= 122 || //Read lower letters
                key.Key == ConsoleKey.Spacebar)
            {
                Console.Write(key.KeyChar);
                if(Console.CursorLeft > outputString.Count)
                {
                    outputString.Add(' ');
                }
                outputString[Console.CursorLeft - 1] = key.KeyChar;
            }
            //This will remove character
            else if (key.Key == ConsoleKey.Backspace && Console.CursorLeft > 1)
            {
                ClearCurrentConsoleLine();
                outputString.RemoveAt(Console.CursorLeft - 1);
                var currentLeft = Console.CursorLeft;
                Console.CursorLeft = 0;
                Console.Write(outputString.Select(x => x.ToString()).Aggregate((x,y) => x + y));
                Console.CursorLeft = currentLeft - 1;
            }
            //This ends input loop
            //You can add more keys to break current loop with OR condition
            else if (key.Key == ConsoleKey.Enter)
            {
                break;
            }
        }
        //Display result and wait for any key
        Console.WriteLine(outputString.Select(x => x.ToString()).Aggregate((x, y) => x + y));
        Console.ReadKey();
    }

    //Clears current console line and sets cursor at where it was before
    public static void ClearCurrentConsoleLine()
    {
        int currentLineCursorTop = Console.CursorTop;
        int currentLineCursorLeft = Console.CursorLeft;
        Console.SetCursorPosition(0, Console.CursorTop);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(currentLineCursorLeft, currentLineCursorTop);
    }

您可以使用Console.CursorLeft = 0; 為了這。 Console.SetCursorPosition相比,優點是您不必評估y坐標,因此光標停留在當前行。

我想為此做一個例子(在兩種情況下,我們都希望光標移至行首):

Console.SetCursorPosition(0, Console.CursorTop); //With SetCursorPosition
Console.CursorLeft = 0; //With CursorLeft

但是,我認為沒有一種讓用戶刪除程序編寫的字符的優雅方法。 您仍然可以不斷檢查用戶是否按下Backshift鍵,然后刪除最后一個字符並向后移動光標,同時您必須異步檢查不會導致什么好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM