本文介绍了结合ConsoleModifiers和ConsoleKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的 C#键,我会很感激你的贡献。

I'm fairly new to C# and I'd appreciate your contribution.

目前,我有我的下面一行代码:
,而(!Console.ReadKey()键= ConsoleKey.Q){} // Q上退出

At the moment, I have the following line on my code: while (Console.ReadKey().Key != ConsoleKey.Q) { } //Exit on Q

因此,大家可以看到,在一个控制台应用程序,如果用户按下问:应用程序退出。精细。现在的问题是:有没有什么办法来组合一个 ConsoleModifiers 加上 ConsoleKey ?而不是使用问:键,我宁愿使用按Ctrl + Q 退出。

So as you can see, on a Console Application, if the user presses Q the application exits. Fine. Now, the question is: is there any way to combine a ConsoleModifiers plus ConsoleKey? Instead of using the Q key, I'd prefer to use the Ctrl+Q on exit.

我为什么要做到这一点的原因是,我不觉得舒适离开只有一个退出键(使得容易防止有人按下它意外)。 Altough语法不正确完全,这就是我大致想实现什么:
ConsoleModifiers.Control + ConsoleKey.Q

The reason why I want to achieve this is that I don't feel confortable to leave only one key on exit (makes easy to prevent that someone presses it accidently). Altough the syntax is completely incorrect, that's what I roughly would like to achieve: ConsoleModifiers.Control + ConsoleKey.Q

先谢谢了。

推荐答案

您可以存储ConsoleKeyInfo变量,并检查这两个参数。

You can store ConsoleKeyInfo in variable and check for both parameters.

ConsoleKeyInfo c;
while ((c = Console.ReadKey()).Modifiers != ConsoleModifiers.Control
       || c.Key != ConsoleKey.Q) { } //Exit on Ctrl + Q

这篇关于结合ConsoleModifiers和ConsoleKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:54