Console.ReadKey(true) 需要两次按键才能显示一个输入
Console.ReadKey(true) needs two key presses to display one input
我正在使用两个线程。一个用于显示一些信息。另一个用于输入处理。目前,我必须输入两次击键才能将其添加到输入中。
我已经尝试过在完成按键操作后锁定输入,但这并没有解决问题。
class min
{
public static void Main(string[] args)
{
min p = new min();
}
public min()
{
DetailsMenu(1);
}
List<string> active = new List<string>() { "Example1", "Example2", "Example3" };
string input = "";
int index;
/// <summary>
/// Creates a detail menu
/// </summary>
/// <param name="index">What name</param>
public void DetailsMenu(int index)
{
Console.CursorVisible = false;
this.index = index;
Thread Input = new Thread(new ThreadStart(DetailMenuInput));
Thread Details = new Thread(new ThreadStart(DetailsMenuThread));
Input.Start();
Details.Start();
}
/// <summary>
/// Displays the screen it self
/// </summary>
void DetailsMenuThread()
{
while (true)
{
Console.Clear();
Console.WriteLine($"{active[index - 1]}");
Console.SetCursorPosition(0, Console.WindowHeight - 1);
Console.WriteLine($">{input}_");
Console.SetWindowPosition(0, 0);
Thread.Sleep(100);
}
}
/// <summary>
/// Needs to handle input
/// in this function i need to press a key twice to add it to input
/// </summary>
void DetailMenuInput()
{
while (true)
{
Console.TreatControlCAsInput = false;
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Backspace)
{
if (input.Length > 0)
input = input.Remove(input.Length - 1);
continue;
}
input += Console.ReadKey(true).KeyChar.ToString();
}
}
}
我想要的是:输入一个字符只需要一个按键。
发生了什么:我需要按两次键才能将其添加到输入字符串中。
当我运行此代码时没有显示任何错误消息。
What I would like: for only one key stroke to be needed for one character to be input.
What happens instead: I need to press the key twice to add it to the input string.
您需要按两次键,因为您的循环在接受输入之前会读取两次键。事实上,唯一真正被视为真正输入的键是第二次按键。第一个可以是退格键以外的任何东西。
不用读取密钥两次,只需使用包含读取的原始密钥的 key
变量:
void DetailMenuInput()
{
while (true)
{
Console.TreatControlCAsInput = false;
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Backspace)
{
if (input.Length > 0)
input = input.Remove(input.Length - 1);
continue;
}
input += key.KeyChar.ToString();
}
}
我正在使用两个线程。一个用于显示一些信息。另一个用于输入处理。目前,我必须输入两次击键才能将其添加到输入中。
我已经尝试过在完成按键操作后锁定输入,但这并没有解决问题。
class min
{
public static void Main(string[] args)
{
min p = new min();
}
public min()
{
DetailsMenu(1);
}
List<string> active = new List<string>() { "Example1", "Example2", "Example3" };
string input = "";
int index;
/// <summary>
/// Creates a detail menu
/// </summary>
/// <param name="index">What name</param>
public void DetailsMenu(int index)
{
Console.CursorVisible = false;
this.index = index;
Thread Input = new Thread(new ThreadStart(DetailMenuInput));
Thread Details = new Thread(new ThreadStart(DetailsMenuThread));
Input.Start();
Details.Start();
}
/// <summary>
/// Displays the screen it self
/// </summary>
void DetailsMenuThread()
{
while (true)
{
Console.Clear();
Console.WriteLine($"{active[index - 1]}");
Console.SetCursorPosition(0, Console.WindowHeight - 1);
Console.WriteLine($">{input}_");
Console.SetWindowPosition(0, 0);
Thread.Sleep(100);
}
}
/// <summary>
/// Needs to handle input
/// in this function i need to press a key twice to add it to input
/// </summary>
void DetailMenuInput()
{
while (true)
{
Console.TreatControlCAsInput = false;
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Backspace)
{
if (input.Length > 0)
input = input.Remove(input.Length - 1);
continue;
}
input += Console.ReadKey(true).KeyChar.ToString();
}
}
}
我想要的是:输入一个字符只需要一个按键。
发生了什么:我需要按两次键才能将其添加到输入字符串中。
当我运行此代码时没有显示任何错误消息。
What I would like: for only one key stroke to be needed for one character to be input.
What happens instead: I need to press the key twice to add it to the input string.
您需要按两次键,因为您的循环在接受输入之前会读取两次键。事实上,唯一真正被视为真正输入的键是第二次按键。第一个可以是退格键以外的任何东西。
不用读取密钥两次,只需使用包含读取的原始密钥的 key
变量:
void DetailMenuInput()
{
while (true)
{
Console.TreatControlCAsInput = false;
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Backspace)
{
if (input.Length > 0)
input = input.Remove(input.Length - 1);
continue;
}
input += key.KeyChar.ToString();
}
}