Пример #1
0
 /// <summary>
 /// handles left arrow key user key press.
 /// </summary>
 private static void handleInputLeftArrowKey()
 {
     // if not at beginning of line, move cursor one character backwards
     if (!ConsoleIOUtils.IsCursorPointingToBeginningOfLine())
     {
         ConsoleIOUtils.MoveCursorHorizontal(-1);
     }
 }
Пример #2
0
            /// <summary>
            /// handles backspace key press. removes character to the left of cursor from console.
            /// </summary>
            private static void handleInputBackspaceKey()
            {
                if (ConsoleIOUtils.CursorLeft > 0) // not at beginning of line
                {
                    // move cursor one character backwards
                    ConsoleIOUtils.MoveCursorHorizontal(-1);

                    // remove current character from line buffer
                    removeCharacterFromLineBufferAtCurrentPosition();

                    // rewrite console input line without removed character
                    rewriteInputLineToCosole();
                }
            }
Пример #3
0
            /// <summary>
            /// handles key press encapsulated in <paramref name="consoleKeyInfo"/>,
            /// and dislays its textual representation to console, if one exists.
            /// </summary>
            /// <param name="consoleKeyInfo"></param>
            internal static void HandleInputKey(ConsoleKeyInfo consoleKeyInfo)
            {
                if (consoleKeyInfo.Key == ConsoleKey.Enter) // key represents enter
                {
                    handleInputEnterKey();
                }
                else if (consoleKeyInfo.Key == ConsoleKey.Backspace) // key represents backspace
                {
                    handleInputBackspaceKey();
                }
                else if (consoleKeyInfo.Key == ConsoleKey.LeftArrow) // key represents left-arrow
                {
                    handleInputLeftArrowKey();
                }
                else if (consoleKeyInfo.Key == ConsoleKey.RightArrow) // key represents right-arrow
                {
                    handleInputRightArrowKey();
                }
                // key has textual representation
                else if (
                    ConsoleIOUtils.IsTextualKey(consoleKeyInfo) &&
                    ConsoleIOUtils.CursorLeft < ConsoleIOUtils.WindowWidth - 1)
                {
                    // insert key char representation to line buffer
                    insertToLineBufferAtCurrentPosition(consoleKeyInfo.KeyChar.ToString());

                    if (ConsoleIOUtils.CursorLeft == inputLineBuffer.Length - 1)  // append to end of line
                    {
                        Console.Write(consoleKeyInfo.KeyChar.ToString());
                    }
                    else // insert within line
                    {
                        // rewrite current line so that insert within line buffer is visible
                        rewriteInputLineToCosole();

                        // move 1 character to the right
                        ConsoleIOUtils.MoveCursorHorizontal(1);
                    }
                }
            }