(view source code of sendkeys.cs as plain text)
using System;
using System.Collections.Generic;
namespace RobvanderWoude
{
class SendKeys
{
static readonly string progver = "1.00";
static int Main( string[] args )
{
if ( args.Length == 1 )
{
switch ( args[0].ToUpper( ) )
{
case "/?":
return ShowHelp( );
case "/FLUSH":
System.Windows.Forms.SendKeys.Flush( );
break;
case "/TABLE":
return ShowKeys( );
default:
System.Windows.Forms.SendKeys.SendWait( args[0] );
break;
}
return 0;
}
else
{
return ShowHelp( );
}
}
#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
/*
SendKeys.exe, Version 1.00
Batch tool to send keystrokes to the active window
Usage: SendKeys.exe keys
or: SendKeys.exe /FLUSH
or: SendKeys.exe /TABLE
Where: keys is the key sequence to be sent to the active window
(for special keys see SENDKEYS.EXE /TABLE output)
/FLUSH wait for the active window to process keystrokes
in the message queue
/TABLE show the special keys table
Notes: The program will wait for its own key sequence to be processed.
Return code -1 in case of errors, otherwise 0.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "SendKeys.exe, Version {0}", progver );
Console.Error.WriteLine( "Batch tool to send keystrokes to the active window" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "SendKeys.exe keys" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "SendKeys.exe /FLUSH" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "SendKeys.exe /TABLE" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Write( "keys " );
Console.ResetColor( );
Console.WriteLine( "is the key sequence to be sent to the active window" );
Console.Error.Write( " (for special keys see " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "SENDKEYS.EXE /TABLE" );
Console.ResetColor( );
Console.Error.WriteLine( " output)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Write( " /FLUSH " );
Console.ResetColor( );
Console.WriteLine( "wait for the active window to process keystrokes" );
Console.Error.WriteLine( " in the message queue" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /TABLE " );
Console.ResetColor( );
Console.Error.WriteLine( "show the special keys table" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: The program will wait for its own key sequence to be processed." );
Console.Error.WriteLine( " Return code -1 in case of errors, otherwise 0." );
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;
}
static int ShowKeys( )
{
// Help text by Microsoft:
// https://docs.microsoft.com/dotnet/api/system.windows.forms.sendkeys.send
Console.Error.Write( "The plus sign (" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "+" );
Console.ResetColor( );
Console.Error.Write( "), caret (" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "^" );
Console.ResetColor( );
Console.Error.Write( "), percent sign (" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "%" );
Console.ResetColor( );
Console.Error.Write( "), tilde (" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "~" );
Console.ResetColor( );
Console.Error.Write( "), and parentheses " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "()" );
Console.ResetColor( );
Console.Error.WriteLine( "have special meanings to SendKeys." );
Console.Error.Write( "To specify one of these characters, enclose it within braces (" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "{}" );
Console.ResetColor( );
Console.Error.WriteLine( ")." );
Console.Error.Write( "For example, to specify the plus sign, use \"" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "{+}" );
Console.ResetColor( );
Console.Error.WriteLine( "\"." );
Console.Error.Write( "To specify brace characters, use \"" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "{{}" );
Console.ResetColor( );
Console.Error.Write( "\" and \"" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "{}}" );
Console.ResetColor( );
Console.Error.WriteLine( "\"." );
Console.Error.WriteLine( "Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them" );
Console.Error.WriteLine( "in braces. In other applications, brackets do have a special meaning that might" );
Console.Error.WriteLine( "be significant when dynamic data exchange (DDE) occurs." );
Console.Error.WriteLine( );
Console.Error.Write( "To specify characters that aren't displayed when you press a key, such as " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "ENTER" );
Console.ResetColor( );
Console.Error.Write( "or " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "TAB" );
Console.ResetColor( );
Console.Error.WriteLine( ", and keys that represent actions rather than characters, use the codes" );
Console.Error.WriteLine( "in the following table." );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "Key Code" );
Console.Error.WriteLine( "=== ====" );
Console.Error.WriteLine( "BACKSPACE {BACKSPACE}, {BS} or {BKSP}" );
Console.Error.WriteLine( "BREAK {BREAK}" );
Console.Error.WriteLine( "CAPS LOCK {CAPSLOCK}" );
Console.Error.WriteLine( "DEL or DELETE {DELETE} or {DEL}" );
Console.Error.WriteLine( "DOWN ARROW {DOWN}" );
Console.Error.WriteLine( "END {END}" );
Console.Error.WriteLine( "ENTER {ENTER} or ~" );
Console.Error.WriteLine( "ESC {ESC}" );
Console.Error.WriteLine( "HELP {HELP}" );
Console.Error.WriteLine( "HOME {HOME}" );
Console.Error.WriteLine( "INS or INSERT {INSERT} or {INS}" );
Console.Error.WriteLine( "LEFT ARROW {LEFT}" );
Console.Error.WriteLine( "NUM LOCK {NUMLOCK}" );
Console.Error.WriteLine( "PAGE DOWN {PGDN}" );
Console.Error.WriteLine( "PAGE UP {PGUP}" );
Console.Error.WriteLine( "PRINT SCREEN {PRTSC} (reserved for future use)" );
Console.Error.WriteLine( "RIGHT ARROW {RIGHT}" );
Console.Error.WriteLine( "SCROLL LOCK {SCROLLLOCK}" );
Console.Error.WriteLine( "TAB {TAB}" );
Console.Error.WriteLine( "UP ARROW {UP}" );
Console.Error.WriteLine( "F1 {F1}" );
Console.Error.WriteLine( "F2 {F2}" );
Console.Error.WriteLine( " . . ." );
Console.Error.WriteLine( "F16 {F16}" );
Console.Error.WriteLine( "Keypad add {ADD}" );
Console.Error.WriteLine( "Keypad subtract {SUBTRACT}" );
Console.Error.WriteLine( "Keypad multiply {MULTIPLY}" );
Console.Error.WriteLine( "Keypad divide {DIVIDE}" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "To specify keys combined with any combination of the SHIFT, CTRL and" );
Console.Error.WriteLine( "ALT keys, precede the key code with one or more of the following codes." );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "Key Code" );
Console.Error.WriteLine( "=== ====" );
Console.Error.WriteLine( "SHIFT +" );
Console.Error.WriteLine( "CTRL ^" );
Console.Error.WriteLine( "ALT %" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "To specify that any combination of " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "SHIFT" );
Console.ResetColor( );
Console.Error.Write( ", " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "CTRL" );
Console.ResetColor( );
Console.Error.Write( " and " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "ALT" );
Console.ResetColor( );
Console.Error.WriteLine( " should be held down while" );
Console.Error.WriteLine( "several other keys are pressed, enclose the code for those keys in parentheses." );
Console.Error.Write( "For example, to specify to hold down " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "SHIFT" );
Console.ResetColor( );
Console.Error.Write( " while " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "E" );
Console.ResetColor( );
Console.Error.Write( " and " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "C" );
Console.ResetColor( );
Console.Error.WriteLine( " are pressed, use" );
Console.Error.Write( "\"" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "+(EC)" );
Console.ResetColor( );
Console.Error.Write( "\"; to specify to hold down " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "SHIFT" );
Console.ResetColor( );
Console.Error.Write( " while " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "E" );
Console.ResetColor( );
Console.Error.Write( " is pressed, followed by " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "C" );
Console.ResetColor( );
Console.Error.WriteLine( " without" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "SHIFT" );
Console.ResetColor( );
Console.Error.Write( ", use \"" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "+EC" );
Console.ResetColor( );
Console.Error.WriteLine( "\"." );
Console.Error.WriteLine( );
Console.Error.Write( "To specify repeating keys, use the form " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "{key number}" );
Console.ResetColor( );
Console.Error.WriteLine( ". You must put a space between" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "key" );
Console.ResetColor( );
Console.Error.Write( " and " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "number" );
Console.ResetColor( );
Console.Error.WriteLine( "." );
Console.Error.Write( "E.g. " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "{UP 42}" );
Console.ResetColor( );
Console.Error.Write( " means press the " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "UP ARROW" );
Console.ResetColor( );
Console.Error.Write( " key " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "42" );
Console.ResetColor( );
Console.Error.Write( " times; " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "{h 10}" );
Console.ResetColor( );
Console.Error.Write( " means press " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "H 10" );
Console.ResetColor( );
Console.Error.WriteLine( " times." );
Console.Error.WriteLine( );
Console.Error.Write( "Source: " );
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Error.WriteLine( "https://docs.microsoft.com/dotnet/api/system.windows.forms.sendkeys.send" );
Console.ResetColor( );
return -1;
}
#endregion Error handling
}
}
page last modified: 2024-04-16; loaded in 0.0116 seconds