Exemple #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));
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="nEvents"></param>
        /// <returns></returns>
        public ConsoleInputEventInfo[] ReadEvents(int nEvents)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }
            ConsoleInputEventInfo[] events = new ConsoleInputEventInfo[nEvents];
            int eventsRead = 0;

            if (!WinCon.ReadConsoleInput(Handle, events, nEvents, ref eventsRead))
            {
                throw new System.IO.IOException("Unable to read events.", Marshal.GetLastWin32Error());
            }
            if (eventsRead < nEvents)
            {
                // create a new array that contains just the events that were read
                ConsoleInputEventInfo[] newBuff = new ConsoleInputEventInfo[eventsRead];
                Array.Copy(events, newBuff, eventsRead);
                events = newBuff;
            }

            return(events);
        }