C# - 控制台击键
-
02-10-2019 - |
题
我想比较控制台中按键的键,如果它们相等,则按照键的键是左箭头键,请将控制台的背景颜色更改为青色...
我不确定如何设置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;
}
不隶属于 StackOverflow