gpt4 book ai didi

C# 控制台 : How to ReadLine without the need of pressing [Enter]

转载 作者:太空狗 更新时间:2023-10-30 01:01:42 24 4
gpt4 key购买 nike

我的 c# 控制台应用程序用作我的 c# 表单应用程序的登录,问题是,在我的 c# 控制台应用程序中,我一直无法找到一种方法来 ReadLine 不需要按 Enter 因为我需要检测是否按下 F2Enter 然后 ReadLine 不需要用户按 再次输入。例如,如果我想检测 F2 是否被按下,我需要等到 F2 被按下,直到我能够 ReadLine,希望这个问题的措辞是有意义的,我相信你可以看出我在 c# 方面是个“菜鸟”。
我的问题示例:

static void Main()
{
var KP = Console.ReadKey();
if (KP.Key == ConsoleKey.F2)
{
//User Presses F2
}
else if (KP.Key == ConsoleKey.Enter)
{
string UserName = ReadLineWithoutPressingEnter();//Just a example
//ReadLine without needing to press enter again
}
}




感谢您的宝贵时间。

最佳答案

保存 ReadKey 的结果,然后只做一个 ReadLine:

   public static void Main(string[] args)
{
var KP = Console.ReadKey();
if (KP.Key == ConsoleKey.F2)
{
return;
}

string UserName = KP.KeyChar + Console.ReadLine();

Console.WriteLine(UserName);
Console.ReadLine();
}

关于C# 控制台 : How to ReadLine without the need of pressing [Enter],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37354142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com