2010-07-02 5 views
2

콘솔에서 누른 키를 왼쪽 화살표 키와 비교하려면 왼쪽 화살표 키를 눌렀을 때 콘솔의 배경색을 시안 색으로 변경하십시오.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"; 
     } 
    } 
} 

} 

답변

4

이 시도 :

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

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

을! 고맙습니다! – user377419

1

당신은 (대신 .KeyChar의) keypress.Key를 사용해야합니다 - 또한 "Cyan" 너무 ConsoleColors.Cyan을해야합니다.

0

이 시도 : 작동

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