private void KeyDown(object sender, KeyEventArgs e)
        {
            if ((txt.Selection.Start.iLine < txt.LinesCount - 1 || txt.Selection.Start.iChar < 2) &&
                ((char)e.KeyData == 13 || e.KeyData == (Keys.Alt | Keys.V) || e.KeyData == (Keys.Alt | Keys.X) ||
                 ((char)e.KeyData != ' ' && (char)e.KeyData != '\t' && !char.IsControl((char)e.KeyData) && (Control.ModifierKeys & ~Keys.Shift) == 0)))
            {
                if (e.KeyData == (Keys.Alt | Keys.X))
                {
                    txt.Copy();
                    e.Handled = true;
                }
                else
                {
                    txt.Selection = lastselection;
                }
            }

            switch (e.KeyCode)
            {
            /*case Keys.LButton: // Mac bugfix
             *      if((e.Modifiers & Keys.Shift) != 0)
             *              txt.Selection = new Range(txt, 0, txt.Selection.Start.iLine, txt.Selection.End.iChar, txt.Selection.End.iLine);
             *      else
             *              txt.Selection = new Range(txt, 0, txt.Selection.Start.iLine, 0, txt.Selection.Start.iLine);
             *      e.Handled = true;
             *      break;
             * case Keys.MButton: // Mac bugfix
             *      if((e.Modifiers & Keys.Shift) != 0)
             *              txt.Selection = new Range(txt, txt.GetLineLength(txt.Selection.End.iLine), txt.Selection.End.iLine, txt.Selection.End.iChar, txt.Selection.End.iLine);
             *      else
             *              txt.Selection = new Range(txt, txt.GetLineLength(txt.Selection.End.iLine), txt.Selection.End.iLine, txt.GetLineLength(txt.Selection.End.iLine), txt.Selection.End.iLine);
             *      e.Handled = true;
             *      break;*/

            case Keys.Up:
            {
                string current = txt.Lines[txt.LinesCount - 1].Substring(2);
                if (HistoryUp(ref current))
                {
                    ReplaceLastLineText(current);
                }
            }
                e.Handled = true;
                break;

            case Keys.Down:
            {
                string current = txt.Lines[txt.LinesCount - 1].Substring(2);
                if (HistoryDown(ref current))
                {
                    ReplaceLastLineText(current);
                }
            }
                e.Handled = true;
                break;

            case Keys.Left:
            case Keys.Back:
                if (txt.Selection.Start.iChar < 2 || (txt.Selection.Start.iChar == 2 && txt.SelectionLength == 0))
                {
                    e.Handled = true;
                }
                break;

            case Keys.Enter:
            case Keys.Cancel:             // Mac num-block return key
                txt.GoEnd();
                string method = txt.Lines[txt.LinesCount - 1].Substring(2);

                string output = Execute(method);

                /*if(method.Equals("clear"))
                 * {
                 *      txt.Clear();
                 *      txt.AppendText("> ");
                 *      txt.SelectionStart += 2;
                 *      e.Handled = true;
                 *      break;
                 * }
                 *
                 * if(output != null && output != "")
                 * {
                 *      //txt.AppendText('\n' + output);
                 *      string[] lines = output.Split('\n');
                 *      foreach(string line in lines)
                 *              txt.AppendText('\n' + line);
                 *      txt.GoEnd();
                 * }
                 *
                 * txt.AppendText("\n");
                 * txt.AppendText("> ");
                 * txt.SelectionStart += 3;
                 * //new Range(txt, 0, txt.Selection.Start.iLine, txt.Selection.Start.iChar, txt.Selection.Start.iLine).ReadOnly = true;*/

                printMethod = false;                 // Printing method is only required when playing back
                ActionManager.Do(PrintCommandAction, method, output);
                printMethod = true;

                e.Handled = true;
                break;

            default:
                if ((e.Modifiers & Keys.Control) != 0 && e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
                {
                    if ((e.Modifiers & Keys.Shift) != 0)
                    {
                        string current = txt.Lines[txt.LinesCount - 1].Substring(2);
                        StoreMacro(e.KeyCode - Keys.D0, current);
                        PrintOutput(string.Format("Macro {0} set to \"{1}\"", e.KeyCode - Keys.D0, current));
                    }
                    else
                    {
                        ReplaceLastLineText(RecallMacro(e.KeyCode - Keys.D0));
                    }
                    break;
                }
                break;
            }
        }