C# 如何在控制台中获取不同按键的按键事件
我试着用谷歌搜索,但我找不到答案,希望这不仅仅是因为我的搜索技能不好。但我目前拥有的是:C# 如何在控制台中获取不同按键的按键事件,c#,keypress,C#,Keypress,我试着用谷歌搜索,但我找不到答案,希望这不仅仅是因为我的搜索技能不好。但我目前拥有的是: static void Main(string[] args) { while (xa == true) { switch (switchNum) { case 0: Console.WriteLine("Text
static void Main(string[] args)
{
while (xa == true)
{
switch (switchNum)
{
case 0:
Console.WriteLine("Text");
break;
case 1:
Console.WriteLine("Stuff");
break;
case 2:
Console.WriteLine("More Stuff");
break;
}
}
我想知道的是,当我按下一个键时,如何改变switchNum。希望每个案例有3个不同的键
编辑:澄清
因此,当前它将在控制台中键入如下内容:
Text
Text
Text
Text
Text
Text
当我按下一个键时,我想让它变成其他的东西,例如w
Text
Text
Text
Text
//Press w here
Stuff
Stuff
Stuff
Stuff
//Pressing another key e.g. q
More Stuff
More Stuff
More Stuff
...
所以我不知道如何接收不同的密钥,并保持循环,因此,readkey可能无法工作
提前谢谢 您需要使用Console.ReadKey查看按键的时间,由于该函数是一个阻塞函数,您可以使用Console.KeyAvailable仅在您知道用户正在按键时调用它。 你可以阅读更多和更多 您的代码可以更改为:
//console input is read as a char, can be used in place of or converted to old switchNum you used
char input = 'a';//define default so the switch case can use it
while (xa == true)
{
//If key is pressed read what it is.
//Use a while loop to clear extra key presses that may have queued up and only keep the last read
while (Console.KeyAvailable) input = Console.ReadKey(true).KeyChar;
//the key read has been converted to a char matching that key
switch (input)
{
case 'a':
Console.WriteLine("Text");
break;
case 's':
Console.WriteLine("Stuff");
break;
case 'd':
Console.WriteLine("More Stuff");
break;
}
}
编辑:正如Jane所评论的,您使用的循环没有限制,它将以运行它的CPU的速度循环。您应该在循环中包含System.Threading.Thread.Sleep,以便在每个周期之间休息,或者在打印前等待一个键,将while Console.KeyAvailable key=Console.ReadKeytrue.KeyChar转换为just key=Console.ReadKeytrue.KeyChar 使用Console.Write\b;在输入的键上退格 然而@Skean的解决方案很好。根据他的回答:-
static void Main(string[] args)
{
Console.WriteLine("start");
bool xa = true;
int switchNum = 48; // ASCII for '0' Key
int switchNumBkup = switchNum; // to restore switchNum on ivalid key
while (xa == true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
switchNumBkup = switchNum; // to restore switchNum on ivalid key
switchNum = key.KeyChar; // Get the ASCII of keyPressed
if (key.Key == ConsoleKey.Escape)
{
Console.WriteLine("Esc Key was pressed. Exiting now....");
Console.ReadKey(); // TO Pause before exiting
break;
}
}
switch (switchNum)
{
case 48: //ASCII for 0
Console.WriteLine("Text");
break;
case 49: //ASCII for 1
Console.WriteLine("Stuff");
break;
case 50: //ASCII for 2
Console.WriteLine("More Stuff");
break;
default :
Console.WriteLine("Invalid Key. Press Esc to exit.");
switchNum = switchNumBkup;
break;
}
System.Threading.Thread.Sleep(200) ;
}
Console.WriteLine("Exiting ");
}
你能用readkey来代替吗?你是说每种情况有三个不同的键盘键?我很难理解你的问题。这就是你想要的吗?请提供xa和switchNum的类型声明好吗?xa和switchNum只是我用来使循环和开关工作的整数,我想更改switchNum,以便在这3种情况中进行选择。我正要发布一个答案,但这涵盖了我将要回答的内容。我唯一可能添加到您的示例中的是switch语句之后的一个小的等待序列,这样可怜的控制台文本不会以每分钟一百万英里的速度运行:实际上,添加“System.Threading.Thread.Sleep50”将循环速度降低到最高20个循环/秒,或者将50更改为您希望等待的毫秒数。谢谢!,这是可行的,但只是为了确保,当你写“key”时,你是指输入吗?@JaneS我在循环之间已经有了1秒的延迟,但为了清晰起见删除了它,但无论如何还是要感谢:@Rerxx是的,对不起,变量“key”应该在那里有“input”。使用int,然后将其转换为ASCII,而不仅仅是使用char,有什么好处吗,像斯肯那样?还是因为我的问题中使用了int?不过,感谢您通过重置为有效密钥改进了答案。@Skean您的解决方案很棒。如果用户输入“f”键,应用程序将停止向控制台发送文本,直到输入“a”、“s”或“d”。@Rerxx我使用int只是因为你提到了。