예제 #1
0
        public static unsafe void KeyDown(uint scancode)
        {
            if (!initialized)
            {
                return;
            }

            // actually not correct because capslock does not behave like shift on all characters...

            bool shifted = (Keyboard.LeftShift() || Keyboard.RightShift());

            TextMode.SetAttributes(TextColor.Yellow, TextColor.Black);


            ADC.Keys key = (ADC.Keys)scancode;
            // switch statement doesn't for all cases somehow..
            //switch (key)
            //{
            //case Keys.Insert:
            if (key == Keys.Insert)
            {
                overwrite = !overwrite;
                SetOverwrite(overwrite);
                return;
            }
            //case Keys.Delete:
            else if (key == Keys.Delete)
            {
                return;
            }
            //case Keys.PageUp:
            else if (key == Keys.PageUp)
            {
                TextMode.ScrollPage(-1);
                return;
            }
            //case Keys.PageDown:
            else if (key == Keys.PageDown)
            {
                TextMode.ScrollPage(+1);
                return;
            }
            //case Keys.Backspace:
            else if (key == Keys.Backspace)
            {
                if (Console.textBuffer != null &&
                    textBuffer->Length <= 0)
                {
                    //Beep??
                    return;
                }

                int x, y, width, height;

                TextMode.GetScreenSize(out width, out height);
                TextMode.GetCursor(out x, out y);
                x--;
                if (x < 0)
                {
                    x = width - 1;
                    y--;
                    if (y < 0)
                    {
                        y = 0;
                        return;
                    }
                }
                TextMode.MoveTo(x, y);
                TextMode.WriteChar((byte)' ');

                textBuffer->RemoveAt(textBuffer->Length - 1, 1);

                TextMode.MoveTo(x, y);
                TextMode.RefreshCursor();
                return;
            }
            //case Keys.LeftArrow:
            else if (key == Keys.LeftArrow)
            {
#if !FORBID_ARROW_KEYS
                int x, y, width, height;

                TextMode.GetScreenSize(out width, out height);
                TextMode.GetCursor(out x, out y);
                x = x - 1; if (x < 0)
                {
                    x = 0;
                }
                TextMode.MoveTo(x, y);
                TextMode.RefreshCursor();
#else
                //TODO: Beep?
#endif
                return;
            }
            //case Keys.RightArrow:
            else if (key == Keys.RightArrow)
            {
#if !FORBID_ARROW_KEYS
                int x, y, width, height;

                TextMode.GetScreenSize(out width, out height);
                TextMode.GetCursor(out x, out y);
                x = x + 1; if (x >= width)
                {
                    x = width - 1;
                }
                TextMode.MoveTo(x, y);
                TextMode.RefreshCursor();
#else
                //TODO: Beep?
#endif
                return;
            }
            //case Keys.UpArrow:
            else if (key == Keys.UpArrow)
            {
#if !FORBID_ARROW_KEYS
                int x, y, width, height;

                TextMode.GetScreenSize(out width, out height);
                TextMode.GetCursor(out x, out y);
                y = y - 1; if (y < 0)
                {
                    y = 0;
                }
                TextMode.MoveTo(x, y);
                TextMode.RefreshCursor();
#else
                //TODO: Beep?
#endif
                return;
            }
            //case Keys.DownArrow:
            else if (key == Keys.DownArrow)
            {
#if !FORBID_ARROW_KEYS
                int x, y, width, height;

                TextMode.GetScreenSize(out width, out height);
                TextMode.GetCursor(out x, out y);
                y = y + 1; if (y >= height)
                {
                    y = height - 1;
                }
                TextMode.MoveTo(x, y);
                TextMode.RefreshCursor();
#else
                //TODO: Beep?
#endif
                return;
            }
            //case Keys.Enter:
            else if (key == Keys.Enter)
            {
                TextMode.WriteLine();
                TextMode.ClearToEndOfLine();
                TextMode.RefreshCursor();
                DispatchBuffer();
                return;
            }
            //}

            // Code to fix keymap issue with caps.
            bool upperCase = shifted;
            if (
                (scancode >= 0x10 && scancode <= 0x26) ||
                (scancode == 0x1e || scancode == 0x1f) ||
                (scancode >= 0x2c && scancode <= 0x2f) ||
                (scancode >= 0x30 && scancode <= 0x32))
            {
                upperCase ^= Keyboard.CapsLock();
            }

            byte character = Keyboard.Translate(scancode, upperCase);
            if (character == 0)
            {
                // just so that you can actually see that keyboard input works (& we simply don't know what character you just pressed)...
                TextMode.WriteChar((byte)'?');
                TextMode.Write((int)scancode);
                TextMode.RefreshCursor();
                textBuffer->AppendChar((byte)255);
                return;
            }

            int read  = TextMode.GetReadPosition();
            int write = TextMode.GetWritePosition();
            if (read != write)
            {
                TextMode.SetReadPos(write);
            }

            TextMode.WriteChar(character);
            textBuffer->AppendChar(character);
            TextMode.RefreshCursor();
        }