<small id='DLpX7'></small><noframes id='DLpX7'>

    <bdo id='DLpX7'></bdo><ul id='DLpX7'></ul>
    1. <i id='DLpX7'><tr id='DLpX7'><dt id='DLpX7'><q id='DLpX7'><span id='DLpX7'><b id='DLpX7'><form id='DLpX7'><ins id='DLpX7'></ins><ul id='DLpX7'></ul><sub id='DLpX7'></sub></form><legend id='DLpX7'></legend><bdo id='DLpX7'><pre id='DLpX7'><center id='DLpX7'></center></pre></bdo></b><th id='DLpX7'></th></span></q></dt></tr></i><div id='DLpX7'><tfoot id='DLpX7'></tfoot><dl id='DLpX7'><fieldset id='DLpX7'></fieldset></dl></div>

    2. <tfoot id='DLpX7'></tfoot>
    3. <legend id='DLpX7'><style id='DLpX7'><dir id='DLpX7'><q id='DLpX7'></q></dir></style></legend>

        在控制台应用程序中禁用用户输入

        时间:2023-08-27
      1. <tfoot id='EpSeu'></tfoot>

      2. <small id='EpSeu'></small><noframes id='EpSeu'>

                <tbody id='EpSeu'></tbody>

              • <bdo id='EpSeu'></bdo><ul id='EpSeu'></ul>
                <i id='EpSeu'><tr id='EpSeu'><dt id='EpSeu'><q id='EpSeu'><span id='EpSeu'><b id='EpSeu'><form id='EpSeu'><ins id='EpSeu'></ins><ul id='EpSeu'></ul><sub id='EpSeu'></sub></form><legend id='EpSeu'></legend><bdo id='EpSeu'><pre id='EpSeu'><center id='EpSeu'></center></pre></bdo></b><th id='EpSeu'></th></span></q></dt></tr></i><div id='EpSeu'><tfoot id='EpSeu'></tfoot><dl id='EpSeu'><fieldset id='EpSeu'></fieldset></dl></div>
                <legend id='EpSeu'><style id='EpSeu'><dir id='EpSeu'><q id='EpSeu'></q></dir></style></legend>

                  本文介绍了在控制台应用程序中禁用用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在制作一个基于 C# 控制台文本的游戏,并且因为我希望它看起来更老派,所以我添加了一种效果,使任何文本(描述、教程、对话)看起来都像是正在输入的,并且它看起来像这样:

                  I am making a C# console text-based game, and because I wanted it to look more old-school, I've added an effect so that any text (descriptions, tutorials, dialogues) looks like it's being typed, and it looks like this:

                  public static int pauseTime = 50;
                  
                  class Writer
                  {
                      public void WriteLine(string myText)
                      {
                          int pauseTime = MainClass.time;
                          for (int i = 0; i < myText.Length; i++)
                          {
                                  Console.Write(myText[i]);
                                  System.Threading.Thread.Sleep(pauseTime);
                          }
                          Console.WriteLine("");
                      }
                  }
                  

                  但后来我觉得这可能很烦人,我想添加一个选项来跳过效果并让所有文本立即出现.所以我选择了 Enter 键作为跳过"键,它使文本立即出现,但按 Enter 键也会创建一个新的文本行,打乱文本.

                  But then I thought that this might be annoying and I thought about adding an option to skip the effect and make all the text appear at once. So I chose the Enter key to be the "skip" key, and it makes the text appear at once, but pressing the enter key also creates a new text line, scrambling the text.

                  所以我想以某种方式禁用用户输入,以便用户无法在控制台中写入任何内容.有没有办法,例如,禁用命令提示符(命令提示符不是指 cmd.exe,而是闪烁的_"下划线符号)?

                  So I want to somehow disable user input, so that the user cannot write anything in the console. Is there a way to, for example, disable the command prompt (and by command prompt I don't mean cmd.exe, but the flashing "_" underscore sign)?

                  推荐答案

                  我认为你想要的是 Console.ReadKey(true) 它将拦截按下的键并且不会显示它.

                  I think what you want is Console.ReadKey(true) which will intercept the pressed key and won't display it.

                  class Writer
                  {
                      public void WriteLine(string myText)
                      {
                          for (int i = 0; i < myText.Length; i++)
                          {
                              if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter)
                              {
                                  Console.Write(myText.Substring(i, myText.Length - i));
                                  break;
                              }
                              Console.Write(myText[i]);
                              System.Threading.Thread.Sleep(pauseTime);
                          }
                          Console.WriteLine("");
                      }
                  }
                  

                  来源:MSDN 文章

                  这篇关于在控制台应用程序中禁用用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:c#在不停止应用程序的情况下读取用户输入 下一篇:用 C# 中的每个新线程打开一个新控制台?

                  相关文章

                  最新文章

                1. <i id='JerKR'><tr id='JerKR'><dt id='JerKR'><q id='JerKR'><span id='JerKR'><b id='JerKR'><form id='JerKR'><ins id='JerKR'></ins><ul id='JerKR'></ul><sub id='JerKR'></sub></form><legend id='JerKR'></legend><bdo id='JerKR'><pre id='JerKR'><center id='JerKR'></center></pre></bdo></b><th id='JerKR'></th></span></q></dt></tr></i><div id='JerKR'><tfoot id='JerKR'></tfoot><dl id='JerKR'><fieldset id='JerKR'></fieldset></dl></div>

                      • <bdo id='JerKR'></bdo><ul id='JerKR'></ul>

                      <small id='JerKR'></small><noframes id='JerKR'>

                    1. <legend id='JerKR'><style id='JerKR'><dir id='JerKR'><q id='JerKR'></q></dir></style></legend>
                      <tfoot id='JerKR'></tfoot>