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

编程入门 行业动态 更新时间:2024-10-14 02:24:28
本文介绍了禁用用户输入在控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想提出一个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(""); } }

但转念一想,这可能是讨厌我想过将可以选择跳过的影响,使所有文字出现一次。所以我选择了回车键是跳跃键,它使文本出现一次,但按下回车键还创建了一个新的文本行,争先恐后的文本。

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(真)将拦截按键,并不会显示出来。

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文章

更多推荐

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

本文发布于:2023-11-17 08:53:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609526.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   应用程序   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!