/// <summary> /// /// Sets the current mode of the console device /// /// </summary> /// <param name="consoleHandle"> /// /// Handle to the console device returned by GetInputHandle /// /// </param> /// <param name="mode"> /// /// Mask of mode flags /// /// </param> /// <exception cref="HostException"> /// /// If Win32's SetConsoleMode fails /// /// </exception> internal static void SetMode(ConsoleHandle consoleHandle, ConsoleModes mode) { Dbg.Assert(!consoleHandle.IsInvalid, "consoleHandle is not valid"); Dbg.Assert(!consoleHandle.IsClosed, "ConsoleHandle is closed"); bool result = NativeMethods.SetConsoleMode(consoleHandle.DangerousGetHandle(), (DWORD)mode); if (result == false) { int err = Marshal.GetLastWin32Error(); HostException e = CreateHostException(err, "SetConsoleMode", ErrorCategory.ResourceUnavailable, ConsoleControlStrings.SetModeExceptionTemplate); throw e; } }
public static extern bool GetConsoleMode(ConsoleHandle hConsoleHandle, ref ConsoleModes lpMode);
public static extern bool SetConsoleMode(ConsoleHandle hConsoleHandle, ConsoleModes dwMode);
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out ConsoleModes lpMode);
/// <summary> /// Reads the input from the input buffer. /// </summary> public ReadRef ReadInput(ReadOptions options) { var handle = GetInputBuffer(); uint read = 0; var readRef = new ReadRef(); StringBuilder inputBuffer = new StringBuilder(options.InitialContent); int currentLength = 0; //Save current mode. uint currentMode = 0; ConsoleHostNativeMethods.GetConsoleMode(handle.DangerousGetHandle(), out currentMode); ConsoleModes newMode = (ConsoleModes)currentMode & ~ConsoleModes.WindowInput & ~ConsoleModes.MouseInput & ~ConsoleModes.ProcessedInput; ConsoleHostNativeMethods.SetConsoleMode(handle.DangerousGetHandle(), (uint)newMode); while (true) { var inputs = DotConsoleNative.ReadConsoleKeys(handle, 1, out read); if (inputs != null && inputs.Length > 0) { if (read == 1) { var input = inputs[0]; if (input.KeyEvent.repeatCount == 0) { //for some bizzare reson this can return a key or record //but repeat count is zero so we should not process it in any way. //This can happen if we're after mouse inputs such as mouse move. continue; } if (input.KeyEvent.keyDown == true) { if (input.EventType == (ushort)KeyEventType.KEY_EVENT) { if (input.KeyEvent.virtualKeyCode == (ushort)VirtualInputCode.Return) { break; } if (input.KeyEvent.virtualKeyCode == (ushort)VirtualInputCode.Left) { //set cursor position. //Should we use the cursor position from renderrer ? or perhaps use //the one from the output buffer of the console that we're using ? var currentCursorPosition = parent.GetCursorPosition(); currentCursorPosition.X -= 1; parent.SetCursorPosition(currentCursorPosition); parent.SetBufferPosition(currentCursorPosition); } else if (input.KeyEvent.virtualKeyCode == (ushort)VirtualInputCode.Right) { //set cursor position. //Should we use the cursor position from renderrer ? or perhaps use //the one from the output buffer of the console that we're using ? var currentCursorPosition = parent.GetCursorPosition(); currentCursorPosition.X += 1; parent.SetCursorPosition(currentCursorPosition); parent.SetBufferPosition(currentCursorPosition); } else if (input.KeyEvent.virtualKeyCode == (ushort)VirtualInputCode.Back) { var currentCursorPosition = parent.GetCursorPosition(); currentCursorPosition.X -= 1; inputBuffer.Remove(inputBuffer.Length - 1, 1); if (options.NoEcho == false) { parent.AlterLine(string.Empty, currentCursorPosition.Y, currentCursorPosition.X, 1, options.BackgroundColor, options.ForegroundColor); } parent.SetCursorPosition(currentCursorPosition); parent.SetBufferPosition(currentCursorPosition); } else { if (options.StopOnUpArrow && input.KeyEvent.virtualKeyCode == (ushort)VirtualInputCode.Up) { readRef.IsUpArrow = true; break; } else if (options.StopOnDownArrow && input.KeyEvent.virtualKeyCode == (ushort)VirtualInputCode.Down) { readRef.IsDownArrow = true; break; } if (options.StopChars != null) { if (options.StopChars.Contains(input.KeyEvent.unicodeChar)) { readRef.StopChar = (input.KeyEvent.unicodeChar); break; } } currentLength++; inputBuffer.Append(input.KeyEvent.unicodeChar); if (options.NoEcho == false) { parent.Write(input.KeyEvent.unicodeChar.ToString(), options.BackgroundColor, options.ForegroundColor, false); } //Maximum allowed chars read, break the input loop. if (options.ReadLength.HasValue && currentLength >= options.ReadLength) { break; } } } } } } } //Restore saved mode. ConsoleHostNativeMethods.SetConsoleMode(handle.DangerousGetHandle(), currentMode); readRef.ReadInput = inputBuffer.ToString(); return(readRef); }
public static void SetMode(ConsoleModes mode) => WinApi.SetConsoleMode(inputHandle, mode);
//TODO: Maybe move to ConsoleInput public static void GetMode(ref ConsoleModes mode) => WinApi.GetConsoleMode(inputHandle, ref mode);
static extern bool SetConsoleMode(IntPtr hConsoleHandle, ConsoleModes dwMode);