// Returs true if the value of Key.Character can be displayed. public bool CanDisplay(KeyEventRecord Key) { bool AltKey = Key.VirtualKeyCode == 0x12; bool CtrlKey = Key.VirtualKeyCode == 0x11; bool ShiftKey = Key.VirtualKeyCode == 0x10; // Win and App keys bool SpecialKey = Key.VirtualKeyCode >= 0x5B && Key.VirtualKeyCode <= 0x5D; // Num Lock, Scroll Lock, Caps Lock bool LockKey = Key.VirtualKeyCode == 0x90 || Key.VirtualKeyCode == 0x91 || Key.VirtualKeyCode == 0x14; if (SpecialKey || LockKey || CtrlKey || ShiftKey) { return(false); } // Alt keys are a bit weird in that the Alt-up can contain a valid character, entered via ALT + Numpad if (Key.KeyDown == 0 && !AltKey) { return(false); } return(Key.Character != '\0'); }
public ConsoleKeyInfo ReadKey(bool intercept) { KeyEventRecord KeyEvent = new KeyEventRecord(); if (SavedKey.RepeatCount != 0) { KeyEvent = SavedKey; SavedKey.RepeatCount--; } else { while (true) { KeyEvent = NextEvent(InputRecordType.KeyEvent).KeyEvent; KeyEvent.RepeatCount--; if (CanDisplay(KeyEvent)) { break; } } } if (KeyEvent.RepeatCount != 0) { SavedKey = KeyEvent; } if (!intercept) { ConsoleEx.ScreenBuffer.Write(KeyEvent.Character); } bool Shift = (KeyEvent.ControlKeyState & CtrlKeyState.SHIFT_PRESSED) != 0; bool Ctrl = (KeyEvent.ControlKeyState & (CtrlKeyState.LEFT_CTRL_PRESSED | CtrlKeyState.RIGHT_CTRL_PRESSED)) != 0; bool Alt = (KeyEvent.ControlKeyState & (CtrlKeyState.LEFT_ALT_PRESSED | CtrlKeyState.RIGHT_ALT_PRESSED)) != 0; return(new ConsoleKeyInfo(KeyEvent.Character, (ConsoleKey)KeyEvent.VirtualKeyCode, Shift, Alt, Ctrl)); }