如何在.NET中禁止打印到控制台的按键? [英] How do i supress keypress being printed to console in .NET?

查看:122
本文介绍了如何在.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中禁止打印到控制台的按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆