【问题标题】:Read one key and continue further automatically读取一键并自动继续
【发布时间】:2017-04-09 07:46:41
【问题描述】:

我有这个代码:

var read = Convert.ToInt32(Console.ReadLine()); // Reading Integer from console
if (read == 1) // If read equals some value
{
    // Do Something
}

如果我按下某个键,我想这样做,它会立即走得更远,而无需在控制台中按 enter 进行确认。谢谢

【问题讨论】:

    标签: c# .net console


    【解决方案1】:

    你应该使用Console.ReadKey

    var read = Convert.ToInt32(Console.ReadKey().KeyChar.ToString());
    

    【讨论】:

    • @PolluxPollux 正确..我只是纠正了它。请检查我的更新。
    • 不错!有用!但是我有一个问题,如何隐藏我在控制台中按下的键?
    • 你说的隐藏是什么意思?
    • 如果我按下那个键,它将写入控制台。
    • 这可以通过将值 true 传递给 Cosnole.ReadKey() 来完成,var read = Convert.ToInt32(Console.ReadKey(true).KeyChar.ToString());
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 2021-04-02
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多