(view source code of quickeditmode.cs as plain text)
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace RobvanderWoude
{
internal class QuickEditMode
{
static readonly string progver = "1.01";
public const uint STD_INPUT_HANDLE = unchecked((uint)-10);
public const uint STD_OUTPUT_HANDLE = unchecked((uint)-11);
public const uint STD_ERROR_HANDLE = unchecked((uint)-12);
public const uint ENABLE_MOUSE_INPUT = 0x0010;
public const uint ENABLE_QUICK_EDIT_MODE = 0x0040;
public const uint ENABLE_EXTENDED_FLAGS = 0x0080;
public const uint ENABLE_ECHO_INPUT = 0x0004;
public const uint ENABLE_WINDOW_INPUT = 0x0008;
public const uint ENABLE_INSERT_MODE = 0x0020;
public const uint ENABLE_LINE_INPUT = 0x0002;
public const uint ENABLE_PROCESSED_INPUT = 0x0001;
public const uint ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
static int Main( string[] args )
{
#region Command Line Parsing
bool consoledefault = false;
bool currentconsole = false;
string quickedit = "get";
foreach ( string arg in args )
{
if ( arg == "0" )
{
if ( quickedit != "get" )
{
return ShowHelp( "Duplicate new value on command line" );
}
quickedit = "off";
}
else if ( arg == "1" )
{
if ( quickedit != "get" )
{
return ShowHelp( "Duplicate new value on command line" );
}
quickedit = "on";
}
else if ( arg.ToUpper( ) == "/C" )
{
if ( currentconsole )
{
return ShowHelp( "Duplicate command line switch /C" );
}
currentconsole = true;
}
else if ( arg.ToUpper( ) == "/D" )
{
if ( consoledefault )
{
return ShowHelp( "Duplicate command line switch /D" );
}
consoledefault = true;
}
else if ( arg == "/?" )
{
return ShowHelp( );
}
else
{
return ShowHelp( "Invalid command line argument \"{0}\"", arg );
}
}
if ( !consoledefault )
{
currentconsole = true;
}
if ( quickedit == "get" && consoledefault && currentconsole )
{
return ShowHelp( "Use either /C or /D, not both, when reading the current status" );
}
#endregion Command Line Parsing
#region Get or Set Current Console
int currentstatus = 0;
if ( currentconsole )
{
currentstatus = CurrentConsole( quickedit );
if ( currentstatus == -1 )
{
if ( quickedit == "get" )
{
return ShowHelp( "Failed to get current console's Quick-Edit mode" );
}
else
{
return ShowHelp( "Failed to set current console's Quick-Edit mode" );
}
}
}
#endregion Get or Set Current Console
#region Get or Set Console Default
int defaultstatus = 0;
if ( consoledefault )
{
defaultstatus = ConsoleDefault( quickedit );
if ( defaultstatus == -1 )
{
if ( quickedit == "get" )
{
return ShowHelp( "Failed to get default Quick-Edit mode" );
}
else
{
return ShowHelp( "Failed to set default Quick-Edit mode" );
}
}
}
#endregion Get or Set Console Default
return currentstatus + defaultstatus;
}
static int ConsoleDefault( string quickeditcommand )
{
if ( quickeditcommand == "get" )
{
return (int)Registry.GetValue( "HKEY_CURRENT_USER\\Console", "QuickEdit", -1 );
}
else
{
int newvalue = 0; // disable
if ( quickeditcommand == "on" )
{
newvalue = 1; // enable
}
Registry.SetValue( "HKEY_CURRENT_USER\\Console", "QuickEdit", newvalue, RegistryValueKind.DWord );
if ( (int)Registry.GetValue( "HKEY_CURRENT_USER\\Console", "QuickEdit", -1 ) == newvalue )
{
return 0; // success
}
}
return -1; // failure
}
static int CurrentConsole( string quickeditcommand )
{
IntPtr handle = GetStdHandle( STD_INPUT_HANDLE );
if ( handle != IntPtr.Zero )
{
uint mode = 0;
if ( GetConsoleMode( handle, ref mode ) )
{
if ( quickeditcommand == "get" )
{
if ( ( mode & ENABLE_QUICK_EDIT_MODE ) == 0 )
{
return 0; // not set
}
else
{
return 1; // set
}
}
else
{
mode |= ENABLE_EXTENDED_FLAGS; // enable, required to change Quick-Edit mode
if ( quickeditcommand == "on" )
{
mode |= ENABLE_QUICK_EDIT_MODE; // enable
}
else
{
mode &= ~ENABLE_QUICK_EDIT_MODE; // disable
}
if ( SetConsoleMode( handle, mode ) )
{
return 0; // success
}
}
}
}
return -1; // failure
}
#region Error handling
static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
#region Help Text
/*
QuickEditMode.exe, Version 1.01
Get or set Quick-Edit mode for consoles
Usage: QuickEditMode.exe [ /C | /D ]
or: QuickEditMode.exe [ 0 | 1 ] [ /C ] [ /D ]
Where: 0 disables Quick-Edit mode
1 enables Quick-Edit mode
/C for current console
/D default for all consoles
Notes: Without command line argument 0 or 1 (get mode), return code 0
indicates Quick-Edit mode was not set, return code 1 indicates
it was set.
With command line argument 0 or 1 (set mode), return code 0
indicates Quick-Edit mode was changed successfully.
Return code -1 always indicates an error occurred.
Without command line switches, only the setting for the current
console is read or changed.
With command line switch /D the default setting for all consoles
is read or changed.
Use both /C and /D if you want to change the default as well as the
current console; when reading the current status do not use both.
Besides this help, the program does not generate any screen output.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "QuickEditMode.exe, Version {0}", progver );
Console.Error.WriteLine( "Get or set the current console's Quick-Edit mode" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "QuickEditMode.exe [ /C | /D ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( " or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "QuickEditMode.exe [ 0 | 1 ] [ /C ] [ /D ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "0" );
Console.ResetColor( );
Console.Error.WriteLine( " disables Quick-Edit mode" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " 1" );
Console.ResetColor( );
Console.Error.WriteLine( " enables Quick-Edit mode" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /C" );
Console.ResetColor( );
Console.Error.WriteLine( " for current console" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /D" );
Console.ResetColor( );
Console.Error.WriteLine( " default for all consoles" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: Without command line argument 0 or 1 (get mode), return code 0" );
Console.Error.WriteLine( " indicates Quick-Edit mode was not set, return code 1 indicates" );
Console.Error.WriteLine( " it was set." );
Console.Error.WriteLine( " With command line argument 0 or 1 (set mode), return code 0" );
Console.Error.WriteLine( " indicates Quick-Edit mode was changed successfully." );
Console.Error.WriteLine( " Return code -1 always indicates an error occurred." );
Console.Error.WriteLine( " Without command line switches, only the setting for the current" );
Console.Error.WriteLine( " console is read or changed." );
Console.Error.WriteLine( " With command line switch /D the default setting for all consoles" );
Console.Error.WriteLine( " is read or changed." );
Console.Error.Write( " Use both /C and /D if you want to " );
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.Write( "change" );
Console.ResetColor( );
Console.Error.WriteLine( " the default as well as the" );
Console.Error.Write( " current console; when " );
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.Write( "reading" );
Console.ResetColor( );
Console.Error.Write( " the current status do " );
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.Write( "not" );
Console.ResetColor( );
Console.Error.WriteLine( " use both." );
Console.Error.WriteLine( " Besides this help, the program does not generate any screen output." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Text
return -1;
}
#endregion Error handling
#region DLL Imports
[DllImport( "kernel32.dll" )]
public static extern IntPtr GetStdHandle( uint nStdHandle );
[DllImportAttribute( "kernel32.dll" )]
public static extern bool GetConsoleMode( IntPtr hConsoleInput, ref uint lpMode );
[DllImportAttribute( "kernel32.dll" )]
public static extern bool SetConsoleMode( IntPtr hConsoleInput, uint dwMode );
#endregion DLL Imports
}
}
page last modified: 2024-04-16; loaded in 0.0100 seconds