c# - 在 Mono C# Readline 中捕获 Control+key

标签 c# mono console

有没有办法在 Mono 控制台中使用 Readline() 来捕获 ctrl+?我正在使用 Mono 2.10.2,当我按下 ctrl-d 时,例如,没有任何反应。当我按下 ReadLine() 时,我不知道收到什么信号。

最佳答案

ReadLine() 读取输入到控制台的一行文本

控制键没有任何文本表示,所以它显然没有被“捕获”。要捕获控制键,您可以使用 Console.ReadKey() 函数。

这是来自 documentation page for Console.ReadKey() 的示例.

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

关于c# - 在 Mono C# Readline 中捕获 Control+key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6970235/

相关文章:

c++ - 如何使用 Qt 在 Windows 和 Linux 上捕获 Ctrl+C

c++ - 是否可以使用 CR 之类的东西一次刷新两行文本? (C++)

c# - 检测 Gtk 和 Mono 中的 TextView 变化

Xamarin 垃圾收集器和循环引用

c# - 这个C#代码合法吗?

c# - 在 Linux 中运行用 Mono 编译的 C# 程序

c# - 从 Task<> 获取对象

c# - 如何为 iPhone 制作聊天客户端

c# - .net core mvc 的 Spring boot Autowired 注释等价物

c - dll 中控制台 I/O 的最佳选择