How do I suppress keypress being printed to console in .NET?(如何在 .NET 中禁止将按键打印到控制台?)
问题描述
我正在将一个小型 C++ 控制台游戏移植到 C#,似乎无法阻止按键被打印到控制台.
I'm porting a small C++ console game to C# and it seems that I can't stop key presses from being printed to the console.
在 C++ 中,我使用这种方法获得击键,这也抑制了击键被打印到控制台:
In C++ I get the keystroke with this method, which also suppress the keystrokes from being printed to the console:
bool Game::getInput(char *c)
{
if (_kbhit())
{
*c = _getch();
return true;
}
return false;
}
我尝试在 C# 中做同样的事情:
I tried to do the equivalent in C# by doing:
Key = Console.ReadKey();
但这并不能抑制字符被打印到控制台,从而导致明显的问题.关于如何解决这个问题的任何想法?
But this does not suppress the character from being printed to the console, causing obvious problems. Any ideas on how to remedy this?
推荐答案
你想要 Console.ReadKey(true)
获取用户按下的下一个字符或功能键.按下的键可选地显示在控制台窗口中.
Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.
参数-称为拦截:
确定是否在控制台窗口中显示按下的键.true 不显示按下的键;否则,false.
Determines whether to display the pressed key in the console window. true to not display the pressed key; otherwise, false.
这篇关于如何在 .NET 中禁止将按键打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 .NET 中禁止将按键打印到控制台?
基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
