【发布时间】:2013-12-07 21:34:48
【问题描述】:
如何检查某个键是否按下,如果按下另一个键或未按下任何键,则不执行任何操作?
我想要在 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 键。如果按下 Escape 键,循环将暂停,直到按下 Enter 键。
我试过了:
-
Console.ReadKey()- 将暂停循环,直到按下任意键。 -
Keyboard.IsKeyDown()- 在控制台应用程序中无效。
【问题讨论】:
-
println是 Java。你确定你有正确的标签吗? :P -
快速谷歌搜索给了我这个:stackoverflow.com/questions/1770518/…
-
你需要一个事件,例如KeyUp
-
监听 Console.KeyAvailable 的其他答案很好,但这也可能有帮助:stackoverflow.com/questions/10988197/…
标签: c# .net console-application keydown keyevent