本人的疑惑是Modifiers返回的是ConsoleModifiers,并不是bool型,这是为什么?
请看代码:代码来自这个网页https://msdn.microsoft.com/zh-cn/library/471w8d85(v=vs.110).aspx
using System;
class Example
{
public static void Main()
{
ConsoleKeyInfo cki;
// Prevent example from ending if CTL+C is pressed.
Console.TreatControlCAsInput = true;
Console.WriteLine(“Press any combination of CTL, ALT, and SHIFT, and a console key.”);
Console.WriteLine(“Press the Escape (Esc) key to quit: \n”);
do
{
cki = Console.ReadKey();
Console.Write(” — You pressed “);
if((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write(“ALT+”);
if((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write(“SHIFT+”);
if((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write(“CTL+”);
Console.WriteLine(cki.Key.ToString());
} while (cki.Key != ConsoleKey.Escape);
}
}
请看代码:代码来自这个网页https://msdn.microsoft.com/zh-cn/library/471w8d85(v=vs.110).aspx
using System;
class Example
{
public static void Main()
{
ConsoleKeyInfo cki;
// Prevent example from ending if CTL+C is pressed.
Console.TreatControlCAsInput = true;
Console.WriteLine(“Press any combination of CTL, ALT, and SHIFT, and a console key.”);
Console.WriteLine(“Press the Escape (Esc) key to quit: \n”);
do
{
cki = Console.ReadKey();
Console.Write(” — You pressed “);
if((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write(“ALT+”);
if((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write(“SHIFT+”);
if((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write(“CTL+”);
Console.WriteLine(cki.Key.ToString());
} while (cki.Key != ConsoleKey.Escape);
}
}
解决方案
25
本人给你把 msdn 上的例子重写一下
ConsoleKeyInfo cki; // Prevent example from ending if CTL+C is pressed. Console.TreatControlCAsInput = true; Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key."); Console.WriteLine("Press the Escape (Esc) key to quit: \n"); do { cki = Console.ReadKey(); Console.Write(" -- You pressed "); var x = cki.Modifiers & ConsoleModifiers.Alt; Console.Write("【type of x={0},x={1}-{2}】", x.GetType().FullName, (int)x, x); if ((x) != 0) Console.Write("ALT+"); if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+"); if ((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+"); Console.WriteLine(cki.Key.ToString()); } while (cki.Key != ConsoleKey.Escape);
这里把它所说的“按位组合”再明确打印一下(取特定位的 & 惭怍值)。
25
什么地方说了是你臆造出来的 bool 类型了?