【问题标题】:C# - Console KeystrokesC# - 控制台按键
【发布时间】:2010-07-02 20:59:50
【问题描述】:

我想比较控制台中按下的键与左箭头键是否相等,这意味着按下的键是左箭头键,键将控制台的背景颜色更改为青色...

我不确定如何设置 If 语句,因为我不知道如何在控制台中比较键。

using System;

namespace ConsolePaint
{
class MainClass
{


    public static void Main (string[] args)
    {
        ConsoleKeyInfo keypress;
        keypress = Console.ReadKey(); // read keystrokes 

        if ( keypress.KeyChar == ConsoleKey.LeftArrow )
        {
            Console.BackgroundColor = "Cyan";
        }
    }
}

}

【问题讨论】:

    标签: c# console-application keystrokes


    【解决方案1】:

    试试这个:

    ConsoleKeyInfo keypress;
    keypress = Console.ReadKey(); // read keystrokes 
    
    if (keypress.Key == ConsoleKey.LeftArrow)
    {
        Console.BackgroundColor = ConsoleColor.Cyan;
    }
    

    【讨论】:

      【解决方案2】:

      您需要使用keypress.Key(而不是.KeyChar) - 您的"Cyan" 也应该是ConsoleColors.Cyan

      【讨论】:

        【解决方案3】:

        试试这个:

            ConsoleKeyInfo keypress;
            keypress = Console.ReadKey(); // read keystrokes 
            if ( (int)keypress.Key == (char)ConsoleKey.LeftArrow )
            {
                Console.BackgroundColor = ConsoleColor.Cyan;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-07
          • 2011-01-05
          相关资源
          最近更新 更多