【问题标题】:How to restrict user input in the console [closed]如何限制控制台中的用户输入[关闭]
【发布时间】:2020-09-16 00:16:32
【问题描述】:

我对 c# 还很陌生,想知道如何限制用户从终端输入。照原样,您甚至可以在读取行出现之前键入一些内容并按 Enter 键。有没有办法阻止这种情况?

【问题讨论】:

标签: c# .net terminal


【解决方案1】:

我认为您真正在寻找的是 Console.ReadKey()。

此函数返回一个ConsoleKeyInfo 结构,其中包含有关按下的键的大量信息。我不确定您所说的“限制”来自用户终端的输入是什么意思,但我认为这意味着您希望能够在按下 Enter 之前知道他们按下了哪些键。这将允许您这样做:

ConsoleKeyInfo keyInfo;
do
{
  keyInfo = Console.ReadKey();
  // do whatever you want with keyInfo here,
  // in this case I'll just print it
  Console.Write(keyInfo.KeyChar);
} while (keyInfo.Key != ConsoleKey.Enter);

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 2021-09-21
    • 1970-01-01
    • 2011-10-07
    • 2016-07-07
    • 2018-01-16
    • 2015-11-29
    • 2016-05-26
    • 2014-04-14
    相关资源
    最近更新 更多