繁体   English   中英

C#-控制台按键

[英]C# - Console Keystrokes

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

我不确定如何设置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";
        }
    }
}

}

尝试这个:

ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes 

if (keypress.Key == ConsoleKey.LeftArrow)
{
    Console.BackgroundColor = ConsoleColor.Cyan;
}

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

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM