Esempio n. 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));
        }
Esempio n. 2
0
        public void WriteEvents(EventArgs[] events, int nEvents)
        {
            // convert EventArgs to ConsoleInputEventInfo structures.
            ConsoleInputEventInfo[] consoleEvents = new ConsoleInputEventInfo[nEvents];
            for (int i = 0; i < nEvents; i++)
            {
                EventArgs             e  = events[i];
                ConsoleInputEventInfo ce = new ConsoleInputEventInfo();

                if (e is ConsoleKeyEventArgs)
                {
                    ConsoleKeyEventArgs eKey = e as ConsoleKeyEventArgs;
                    ce.EventType                = ConsoleInputEventType.KeyEvent;
                    ce.KeyEvent.UnicodeChar     = eKey.KeyChar;
                    ce.KeyEvent.ControlKeyState = eKey.ControlKeyState;
                    ce.KeyEvent.KeyDown         = eKey.KeyDown;
                    ce.KeyEvent.RepeatCount     = (short)eKey.RepeatCount;
                    ce.KeyEvent.VirtualKeyCode  = eKey.Key;
                    ce.KeyEvent.VirtualScanCode = (short)eKey.VirtualScanCode;
                }
                else if (e is ConsoleMouseEventArgs)
                {
                    ConsoleMouseEventArgs eMouse = e as ConsoleMouseEventArgs;
                    ce.EventType = ConsoleInputEventType.MouseEvent;
                    ce.MouseEvent.ButtonState     = eMouse.ButtonState;
                    ce.MouseEvent.ControlKeyState = eMouse.KeyState;
                    ce.MouseEvent.EventFlags      = eMouse.EventFlags;
                    ce.MouseEvent.MousePosition   = new Coord((short)eMouse.X, (short)eMouse.Y);
                }
                else if (e is ConsoleFocusEventArgs)
                {
                    ConsoleFocusEventArgs eFocus = e as ConsoleFocusEventArgs;
                    ce.EventType           = ConsoleInputEventType.FocusEvent;
                    ce.FocusEvent.SetFocus = eFocus.SetFocus;
                }
                else if (e is ConsoleMenuEventArgs)
                {
                    ConsoleMenuEventArgs eMenu = e as ConsoleMenuEventArgs;
                    ce.EventType           = ConsoleInputEventType.MenuEvent;
                    ce.MenuEvent.CommandId = eMenu.CommandId;
                }
                else if (e is ConsoleWindowBufferSizeEventArgs)
                {
                    ConsoleWindowBufferSizeEventArgs eWindow = e as ConsoleWindowBufferSizeEventArgs;
                    ce.EventType = ConsoleInputEventType.WindowBufferSizeEvent;
                    ce.WindowBufferSizeEvent.Size = new Coord((short)eWindow.X, (short)eWindow.Y);
                }
                else
                {
                    throw new ApplicationException("Unknown event type.");
                }
                consoleEvents[i] = ce;
            }
            WriteEvents(consoleEvents, consoleEvents.Length);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="Events"></param>
 /// <param name="nEvents"></param>
 /// <returns></returns>
 private int WriteEvents(ConsoleInputEventInfo[] Events, int nEvents)
 {
     if (disposed)
     {
         throw new ObjectDisposedException(this.ToString());
     }
     if (nEvents > Events.Length)
     {
         throw new ArgumentException("Count cannot be larger than array size.", "nEvents");
     }
     int eventsWritten = 0;
     if (!WinCon.WriteConsoleInput(Handle, Events, nEvents, ref eventsWritten))
     {
         throw new IOException("Unable to write events.", Marshal.GetLastWin32Error());
     }
     return eventsWritten;
 }
Esempio n. 5
0
        public void WriteEvents(EventArgs[] events, int nEvents)
        {
            // convert EventArgs to ConsoleInputEventInfo structures.
            ConsoleInputEventInfo[] consoleEvents = new ConsoleInputEventInfo[nEvents];
            for (int i = 0; i < nEvents; i++)
            {
                EventArgs e = events[i];
                ConsoleInputEventInfo ce = new ConsoleInputEventInfo();

                if (e is ConsoleKeyEventArgs)
                {
                    ConsoleKeyEventArgs eKey = e as ConsoleKeyEventArgs;
                    ce.EventType = ConsoleInputEventType.KeyEvent;
                    ce.KeyEvent.UnicodeChar = eKey.KeyChar;
                    ce.KeyEvent.ControlKeyState = eKey.ControlKeyState;
                    ce.KeyEvent.KeyDown = eKey.KeyDown;
                    ce.KeyEvent.RepeatCount = (short)eKey.RepeatCount;
                    ce.KeyEvent.VirtualKeyCode = eKey.Key;
                    ce.KeyEvent.VirtualScanCode = (short)eKey.VirtualScanCode;
                }
                else if (e is ConsoleMouseEventArgs)
                {
                    ConsoleMouseEventArgs eMouse = e as ConsoleMouseEventArgs;
                    ce.EventType = ConsoleInputEventType.MouseEvent;
                    ce.MouseEvent.ButtonState = eMouse.ButtonState;
                    ce.MouseEvent.ControlKeyState = eMouse.KeyState;
                    ce.MouseEvent.EventFlags = eMouse.EventFlags;
                    ce.MouseEvent.MousePosition = new Coord((short)eMouse.X, (short)eMouse.Y);
                }
                else if (e is ConsoleFocusEventArgs)
                {
                    ConsoleFocusEventArgs eFocus = e as ConsoleFocusEventArgs;
                    ce.EventType = ConsoleInputEventType.FocusEvent;
                    ce.FocusEvent.SetFocus = eFocus.SetFocus;
                }
                else if (e is ConsoleMenuEventArgs)
                {
                    ConsoleMenuEventArgs eMenu = e as ConsoleMenuEventArgs;
                    ce.EventType = ConsoleInputEventType.MenuEvent;
                    ce.MenuEvent.CommandId = eMenu.CommandId;
                }
                else if (e is ConsoleWindowBufferSizeEventArgs)
                {
                    ConsoleWindowBufferSizeEventArgs eWindow = e as ConsoleWindowBufferSizeEventArgs;
                    ce.EventType = ConsoleInputEventType.WindowBufferSizeEvent;
                    ce.WindowBufferSizeEvent.Size = new Coord((short)eWindow.X, (short)eWindow.Y);
                }
                else
                {
                    throw new ApplicationException("Unknown event type.");
                }
                consoleEvents[i] = ce;
            }
            WriteEvents(consoleEvents, consoleEvents.Length);
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        public int ReadEvents(ConsoleInputEventInfo[] events)
        {
            if (disposed) {
            throw new ObjectDisposedException(this.ToString());
              }
              int eventsRead = 0;
              if (!WinCon.ReadConsoleInput(Handle, events, events.Length, ref eventsRead)) {
            throw new System.IO.IOException("Unable to read events.", Marshal.GetLastWin32Error());
              }

              return eventsRead;
        }
Esempio n. 8
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;
        }