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