如何检查特定按键是否已关闭,如果按下其他按键或没有按键则不执行任何操作?
我想在Visual C#控制台应用程序中使用类似伪代码的东西:
while (true) {
if (IsKeyDown(Escape)) { //checks if Escape is down
println("Press enter to resume");
waitKey(Enter); //waits until Enter is pressed
}
//do something
}
此循环将继续执行某些操作,直到按下Escape键。如果按下退出键,循环将暂停,直到按下Enter键。
Console.ReadKey()
- 只会暂停循环,直到按任意键为止。Keyboard.IsKeyDown()
- 在控制台应用程序中无效。答案 0 :(得分:1)
这是你在找什么?
using System;
class Example
{
public static void Main()
{
ConsoleKeyInfo cki;
// Prevent example from ending if CTL+C is pressed.
Console.TreatControlCAsInput = true;
Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
Console.WriteLine("Press the Escape (Esc) key to quit: \n");
do
{
cki = Console.ReadKey();
Console.Write(" --- You pressed ");
if((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
if((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
if((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
Console.WriteLine(cki.Key.ToString());
} while (cki.Key != ConsoleKey.Escape);
}
}
http://msdn.microsoft.com/en-us/library/471w8d85%28v=vs.110%29.aspx