0

I have this enum for all four possible arrow key presses

enum Direction{up=ConsoleKey.UpArrow, left=Consolekey.LeftArrow,...};

private ConsoleKeyInfo userSelect;

private bool mQuit;

I then have

     public void getUserInput()
     {
        userSelect = Console.ReadKey()

        if (userSelect.Key == ConsoleKey.Escape)
        {
            mQuit = true;
        }
       else if(userSelect.Key == "check if key press is value in enumeration")
       {
        //implementation
       }
    }

Can't work out what the code would be to check "if Key press is one of the values in enum" Any ideas?

4

2 回答 2

4
else if(Enum.IsDefined(typeof(Direction), userSelect.Key)) {
    //Logic
}
于 2013-03-12T12:05:21.537 回答
0

你可以使用 cast 来解决这个问题。

if (userSelect.Key == (ConsoleKey)myEnum)
{

}
于 2013-03-12T12:14:01.457 回答