Exemplo n.º 1
0
        public ConsoleKeyInfo ReadNextKey()
        {
            var info = new ConsoleKeyInfo();

            if (_disposed)
            {
                return(info);
            }

            if (!InvokeRequired)
            {
                throw new InvalidOperationException("you cannot use ReadNextKey from the UI thread");
            }

            var hold = new ManualResetEvent(false);

            try
            {
                KeyEventHandler handler = (s, a) =>
                {
                    a.Handled           = true;
                    info.VirtualKeyCode = a.KeyValue;
                    info.Character      =
                        (char)a.KeyValue;
                    info.ControlKeyState = GetControlKeyState(a);
                    info.KeyDown         = true;
                    hold.Set();
                };

                KeyDown += handler;
                hold.WaitOne();
                KeyDown -= handler;
            }
            finally
            {
                hold.Close();
            }

            return(info);
        }
Exemplo n.º 2
0
        public ConsoleKeyInfo ReadNextKey()
        {
            var info = new ConsoleKeyInfo();
            if (_disposed)
            {
                return info;
            } 
            
            if (!InvokeRequired)
            {
                throw new InvalidOperationException("you cannot use ReadNextKey from the UI thread");
            }

            var hold = new ManualResetEvent(false);
            try
            {
                KeyEventHandler handler = (s, a) =>
                                              {
                                                  a.Handled = true;
                                                  info.VirtualKeyCode = a.KeyValue;
                                                  info.Character =
                                                      (char)a.KeyValue;
                                                  info.ControlKeyState = GetControlKeyState(a);
                                                  info.KeyDown = true;
                                                  hold.Set();
                                              };

                KeyDown += handler;
                hold.WaitOne();
                KeyDown -= handler;
            }
            finally
            {
                hold.Close();
            }

            return info;
        }