Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

c# - How do I suppress keypress being printed to console in .NET?

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.

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;
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want Console.ReadKey(true)

Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.

The argument - which is called intercept:

Determines whether to display the pressed key in the console window. true to not display the pressed key; otherwise, false.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...