[英]How to check if any of 2 possible keys is pressed with ConsoleKeyInfo?
I encountered a pretty dumb problem with ConsoleKeyInfo. 我在ConsoleKeyInfo上遇到了一个非常愚蠢的问题。 I want do check if "1" is entered using either numpad or regular top numeric keys.
我想检查是否使用数字键盘或常规顶部数字键输入了“ 1”。
ConsoleKeyInfo keyPressed;
keyPressed = Console.ReadKey();
if (keyPressed = ConsoleKey.D1 || keyPressed = ConsoleKey.NumPad1)
{ }
And for some reason I cant use "||" 由于某种原因,我不能使用“ ||” operator.
操作员。 Is it possible to somehow check it within 1 if loop without using
Console.ReadLine();
是否可以在不使用
Console.ReadLine();
情况下以某种方式在1循环内检查它Console.ReadLine();
and forcing user to press enter? 并强迫用户按Enter?
You have to compare with ==
instead of =
. 您必须与
==
而不是=
进行比较 。 Otherwise you're trying to assigning the value. 否则,您将尝试分配值。
And you have to compare the Key property of ConsoleKeyInfo
which holds the ConsoleKey
enum. 而且,您必须比较包含
ConsoleKey
枚举的ConsoleKeyInfo
的Key属性。
So your if
should look like: 所以你的
if
应该看起来像:
if (keyPressed.Key == ConsoleKey.D1 || keyPressed.Key == ConsoleKey.NumPad1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.