예제 #1
0
        /// <summary>
        /// Obtains the next character or function key pressed by the user.
        /// The pressed key is optionally displayed in the console window.
        /// </summary>
        /// <param name="intercept">Determines whether to display the pressed key in the console window.
        /// true to not display the pressed key; otherwise, false.</param>
        /// <returns>A ConsoleKeyInfo object that describes the ConsoleKey constant and Unicode
        /// character, if any, that correspond to the pressed console key.></returns>
        public ConsoleKeyInfo ReadKey(bool intercept)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }
            // @TODO: How to I echo the key?
            ConsoleInputEventInfo[] buff = new ConsoleInputEventInfo[1];
            int  numEvents = 0;
            bool isGood    = false;
            ConsoleKeyEventInfo keyEvent = new ConsoleKeyEventInfo();

            do
            {
                if (!WinCon.ReadConsoleInput(Handle, buff, 1, ref numEvents))
                {
                    throw new IOException("Error reading console.", Marshal.GetLastWin32Error());
                }
                // Make sure it's a key down event and that the key is
                // one of the virtual key codes....
                if (buff[0].EventType == ConsoleInputEventType.KeyEvent)
                {
                    keyEvent = buff[0].KeyEvent;
                    isGood   = keyEvent.KeyDown && (Enum.GetName(typeof(ConsoleKey), (ConsoleKey)keyEvent.VirtualKeyCode) != null);
                }
            } while (!isGood);

            // Create ConsoleKeyInfo from key event
            return(new ConsoleKeyInfo(keyEvent.UnicodeChar,
                                      (ConsoleKey)keyEvent.VirtualKeyCode,
                                      (keyEvent.ControlKeyState & ConsoleControlKeyState.ShiftPressed) != 0,
                                      (keyEvent.ControlKeyState & (ConsoleControlKeyState.RightAltPressed | ConsoleControlKeyState.LeftAltPressed)) != 0,
                                      (keyEvent.ControlKeyState & (ConsoleControlKeyState.RightCtrlPressed | ConsoleControlKeyState.LeftCtrlPressed)) != 0));
        }
예제 #2
0
 internal ConsoleKeyEventArgs(ref ConsoleKeyEventInfo keyEvent)
 {
     bKeyDown        = keyEvent.KeyDown;
     repeatCount     = keyEvent.RepeatCount;
     virtualKeyCode  = keyEvent.VirtualKeyCode;
     virtualScanCode = keyEvent.VirtualScanCode;
     uChar           = keyEvent.UnicodeChar;
     asciiChar       = keyEvent.AsciiChar;
     keyState        = keyEvent.ControlKeyState;
 }
예제 #3
0
 internal ConsoleKeyEventArgs(ref ConsoleKeyEventInfo keyEvent)
 {
     bKeyDown = keyEvent.KeyDown;
     repeatCount = keyEvent.RepeatCount;
     virtualKeyCode = keyEvent.VirtualKeyCode;
     virtualScanCode = keyEvent.VirtualScanCode;
     uChar = keyEvent.UnicodeChar;
     asciiChar = keyEvent.AsciiChar;
     keyState = keyEvent.ControlKeyState;
 }
예제 #4
0
        /// <summary>
        /// Obtains the next character or function key pressed by the user.
        /// The pressed key is optionally displayed in the console window. 
        /// </summary>
        /// <param name="intercept">Determines whether to display the pressed key in the console window. 
        /// true to not display the pressed key; otherwise, false.</param>
        /// <returns>A ConsoleKeyInfo object that describes the ConsoleKey constant and Unicode
        /// character, if any, that correspond to the pressed console key.></returns>
        public ConsoleKeyInfo ReadKey(bool intercept)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }
            // @TODO: How to I echo the key?
            ConsoleInputEventInfo[] buff = new ConsoleInputEventInfo[1];
            int numEvents = 0;
            bool isGood = false;
            ConsoleKeyEventInfo keyEvent = new ConsoleKeyEventInfo();
            do
            {
                if (!WinCon.ReadConsoleInput(Handle, buff, 1, ref numEvents))
                {
                    throw new IOException("Error reading console.", Marshal.GetLastWin32Error());
                }
                // Make sure it's a key down event and that the key is
                // one of the virtual key codes....
                if (buff[0].EventType == ConsoleInputEventType.KeyEvent)
                {
                    keyEvent = buff[0].KeyEvent;
                    isGood = keyEvent.KeyDown && (Enum.GetName(typeof(ConsoleKey), (ConsoleKey)keyEvent.VirtualKeyCode) != null);
                }
            } while (!isGood);

            // Create ConsoleKeyInfo from key event
            return new ConsoleKeyInfo(keyEvent.UnicodeChar,
                (ConsoleKey)keyEvent.VirtualKeyCode,
                (keyEvent.ControlKeyState & ConsoleControlKeyState.ShiftPressed) != 0,
                (keyEvent.ControlKeyState & (ConsoleControlKeyState.RightAltPressed | ConsoleControlKeyState.LeftAltPressed)) != 0,
                (keyEvent.ControlKeyState & (ConsoleControlKeyState.RightCtrlPressed | ConsoleControlKeyState.LeftCtrlPressed)) != 0);
        }