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

Deadly 提交于 2021-01-21 08:24:18

问题


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.


回答1:


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.

e.g.

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();
}



回答2:


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



来源:https://stackoverflow.com/questions/33490168/how-to-check-if-ctrl-key-is-pressed-in-console-application-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!