Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine ConsoleModifiers and ConsoleKey

Tags:

c#

console

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

At the moment, I have the following line on my code: while (Console.ReadKey().Key != ConsoleKey.Q) { } //Exit on 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.

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

Thanks in advance.

like image 807
AuroMetal Avatar asked Dec 01 '25 15:12

AuroMetal


1 Answers

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
like image 132
M.kazem Akhgary Avatar answered Dec 04 '25 07:12

M.kazem Akhgary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!