我有四个可能的箭头键按下这个枚举
enum Direction{up=ConsoleKey.UpArrow, left=Consolekey.LeftArrow,...};
private ConsoleKeyInfo userSelect;
private bool mQuit;
然后我
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
}
}
无法确定代码将检查“如果按键是枚举中的值之一” 有什么想法吗?
答案 0 :(得分:4)
else if(Enum.IsDefined(typeof(Direction), userSelect.Key)) {
//Logic
}
答案 1 :(得分:0)
您可以使用强制转换来解决此问题。
if (userSelect.Key == (ConsoleKey)myEnum)
{
}