简体   繁体   中英

how To check if CTRL key is pressed in console application c#

I'm Going to start a console application. The problem is how to determine that the CTRL key is pressed alone without any other key.

using System;
using System.Text;

public class ConsoleKeyExample
{
   public static void Main()
    {

       ConsoleKeyInfo input;
       do
       {
           input = Console.ReadKey(true);
           StringBuilder output = new StringBuilder(String.Format("You pressed {0}",input.Key.ToString()));

           Console.WriteLine(output.ToString());
           if ((input.Modifiers & ConsoleModifiers.Control) != 0)
           {
               Console.WriteLine("CTRL Pressed");
            }
       } while (input.Key != ConsoleKey.Escape);
   }
  }

I want to monitor the behavior of the CTRL key. After tracing this code, I put a checkpoint on readkey line, but when I press CTRL, nothing happens, but when I press any other key like "K" it starts reading key from the keyboard.

Unless you are using a .Net framework older than version 4.0 I believe using Enum.HasFlag() is more readable than using bit-wise AND.

eg

if (cki.Modifiers.HasFlag(ConsoleModifiers.Control))
{
   Console.Write("CTRL ");
}

I'm not sure why the MSDN article isn't updated to use flags because ConsoleModifiers does support Flags attribute.

Below is an updated copy of the same program that works on framework 4.0 and above.

static void Main(string[] args)
{
    ConsoleKeyInfo cki;

    Console.WriteLine("Press Esc to exit the loop");

    do
    {
        cki = Console.ReadKey(true);

        if (cki.Modifiers.HasFlag(ConsoleModifiers.Control))
        {
            Console.Write("CTRL ");
        }

        if (cki.Modifiers.HasFlag(ConsoleModifiers.Alt))
        {
            Console.Write("ALT ");
        }

        if (cki.Modifiers.HasFlag(ConsoleModifiers.Shift))
        {
            Console.Write("SHIFT ");
        }

        Console.WriteLine(cki.Key.ToString());

    } while (cki.Key != ConsoleKey.Escape);


    Console.WriteLine("Press any key to exit");
    Console.ReadKey();
}

Yes it is possible by using ConsoleKeyInfo . 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);
}

Although applies only to .NET Framework 4.6 and 4.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM