Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sendkeys.cs

(view source code of sendkeys.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class SendKeys
  8. 	{
  9. 		static readonly string progver = "1.00";
  10.  
  11.  
  12. 		static int Main( string[] args )
  13. 		{
  14. 			if ( args.Length == 1 )
  15. 			{
  16. 				switch ( args[0].ToUpper( ) )
  17. 				{
  18. 					case "/?":
  19. 						return ShowHelp( );
  20. 					case "/FLUSH":
  21. 						System.Windows.Forms.SendKeys.Flush( );
  22. 						break;
  23. 					case "/TABLE":
  24. 						return ShowKeys( );
  25. 					default:
  26. 						System.Windows.Forms.SendKeys.SendWait( args[0] );
  27. 						break;
  28. 				}
  29. 				return 0;
  30. 			}
  31. 			else
  32. 			{
  33. 				return ShowHelp( );
  34. 			}
  35. 		}
  36.  
  37.  
  38. 		#region Error handling
  39.  
  40. 		static int ShowHelp( params string[] errmsg )
  41. 		{
  42. 			#region Error Message
  43.  
  44. 			if ( errmsg.Length > 0 )
  45. 			{
  46. 				List<string> errargs = new List<string>( errmsg );
  47. 				errargs.RemoveAt( 0 );
  48. 				Console.Error.WriteLine( );
  49. 				Console.ForegroundColor = ConsoleColor.Red;
  50. 				Console.Error.Write( "ERROR:\t" );
  51. 				Console.ForegroundColor = ConsoleColor.White;
  52. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  53. 				Console.ResetColor( );
  54. 			}
  55.  
  56. 			#endregion Error Message
  57.  
  58.  
  59. 			#region Help Text
  60.  
  61. 			/*
  62. 			SendKeys.exe,  Version 1.00
  63. 			Batch tool to send keystrokes to the active window
  64.  
  65. 			Usage:   SendKeys.exe  keys
  66.  
  67. 			or:      SendKeys.exe  /FLUSH
  68.  
  69. 			or:      SendKeys.exe  /TABLE
  70.  
  71. 			Where:   keys      is the key sequence to be sent to the active window
  72. 			                   (for special keys see SENDKEYS.EXE /TABLE output)
  73. 			         /FLUSH    wait for the active window to process keystrokes
  74. 			                   in the message queue
  75. 			         /TABLE    show the special keys table
  76.  
  77. 			Notes:   The program will wait for its own key sequence to be processed.
  78. 			         Return code -1 in case of errors, otherwise 0.
  79.  
  80. 			Written by Rob van der Woude
  81. 			https://www.robvanderwoude.com
  82. 			*/
  83.  
  84. 			#endregion Help Text
  85.  
  86.  
  87. 			#region Display Help Text
  88.  
  89. 			Console.Error.WriteLine( );
  90.  
  91. 			Console.Error.WriteLine( "SendKeys.exe,  Version {0}", progver );
  92.  
  93. 			Console.Error.WriteLine( "Batch tool to send keystrokes to the active window" );
  94.  
  95. 			Console.Error.WriteLine( );
  96.  
  97. 			Console.Error.Write( "Usage:   " );
  98. 			Console.ForegroundColor = ConsoleColor.White;
  99. 			Console.Error.WriteLine( "SendKeys.exe  keys" );
  100. 			Console.ResetColor( );
  101.  
  102. 			Console.Error.WriteLine( );
  103.  
  104. 			Console.Error.Write( "or:      " );
  105. 			Console.ForegroundColor = ConsoleColor.White;
  106. 			Console.Error.WriteLine( "SendKeys.exe  /FLUSH" );
  107. 			Console.ResetColor( );
  108.  
  109. 			Console.Error.WriteLine( );
  110.  
  111. 			Console.Error.Write( "or:      " );
  112. 			Console.ForegroundColor = ConsoleColor.White;
  113. 			Console.Error.WriteLine( "SendKeys.exe  /TABLE" );
  114. 			Console.ResetColor( );
  115.  
  116. 			Console.Error.WriteLine( );
  117.  
  118. 			Console.Write( "Where:   " );
  119. 			Console.ForegroundColor = ConsoleColor.White;
  120. 			Console.Write( "keys      " );
  121. 			Console.ResetColor( );
  122. 			Console.WriteLine( "is the key sequence to be sent to the active window" );
  123.  
  124. 			Console.Error.Write( "                   (for special keys see " );
  125. 			Console.ForegroundColor = ConsoleColor.White;
  126. 			Console.Error.Write( "SENDKEYS.EXE /TABLE" );
  127. 			Console.ResetColor( );
  128. 			Console.Error.WriteLine( " output)" );
  129.  
  130. 			Console.ForegroundColor = ConsoleColor.White;
  131. 			Console.Write( "         /FLUSH    " );
  132. 			Console.ResetColor( );
  133. 			Console.WriteLine( "wait for the active window to process keystrokes" );
  134.  
  135. 			Console.Error.WriteLine( "                   in the message queue" );
  136.  
  137. 			Console.ForegroundColor = ConsoleColor.White;
  138. 			Console.Error.Write( "         /TABLE    " );
  139. 			Console.ResetColor( );
  140. 			Console.Error.WriteLine( "show the special keys table" );
  141.  
  142. 			Console.Error.WriteLine( );
  143.  
  144. 			Console.Error.WriteLine( "Notes:   The program will wait for its own key sequence to be processed." );
  145.  
  146. 			Console.Error.WriteLine( "         Return code -1 in case of errors, otherwise 0." );
  147.  
  148. 			Console.Error.WriteLine( );
  149.  
  150. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  151.  
  152. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  153.  
  154. 			#endregion Display Help Text
  155.  
  156.  
  157. 			return -1;
  158. 		}
  159.  
  160.  
  161. 		static int ShowKeys( )
  162. 		{
  163. 			// Help text by Microsoft:
  164. 			// https://docs.microsoft.com/dotnet/api/system.windows.forms.sendkeys.send
  165.  
  166. 			Console.Error.Write( "The plus sign (" );
  167. 			Console.ForegroundColor = ConsoleColor.White;
  168. 			Console.Error.Write( "+" );
  169. 			Console.ResetColor( );
  170. 			Console.Error.Write( "), caret (" );
  171. 			Console.ForegroundColor = ConsoleColor.White;
  172. 			Console.Error.Write( "^" );
  173. 			Console.ResetColor( );
  174. 			Console.Error.Write( "), percent sign (" );
  175. 			Console.ForegroundColor = ConsoleColor.White;
  176. 			Console.Error.Write( "%" );
  177. 			Console.ResetColor( );
  178. 			Console.Error.Write( "), tilde (" );
  179. 			Console.ForegroundColor = ConsoleColor.White;
  180. 			Console.Error.Write( "~" );
  181. 			Console.ResetColor( );
  182. 			Console.Error.Write( "), and parentheses " );
  183. 			Console.ForegroundColor = ConsoleColor.White;
  184. 			Console.Error.WriteLine( "()" );
  185. 			Console.ResetColor( );
  186.  
  187. 			Console.Error.WriteLine( "have special meanings to SendKeys." );
  188.  
  189. 			Console.Error.Write( "To specify one of these characters, enclose it within braces (" );
  190. 			Console.ForegroundColor = ConsoleColor.White;
  191. 			Console.Error.Write( "{}" );
  192. 			Console.ResetColor( );
  193. 			Console.Error.WriteLine( ")." );
  194.  
  195. 			Console.Error.Write( "For example, to specify the plus sign, use \"" );
  196. 			Console.ForegroundColor = ConsoleColor.White;
  197. 			Console.Error.Write( "{+}" );
  198. 			Console.ResetColor( );
  199. 			Console.Error.WriteLine( "\"." );
  200.  
  201. 			Console.Error.Write( "To specify brace characters, use \"" );
  202. 			Console.ForegroundColor = ConsoleColor.White;
  203. 			Console.Error.Write( "{{}" );
  204. 			Console.ResetColor( );
  205. 			Console.Error.Write( "\" and \"" );
  206. 			Console.ForegroundColor = ConsoleColor.White;
  207. 			Console.Error.Write( "{}}" );
  208. 			Console.ResetColor( );
  209. 			Console.Error.WriteLine( "\"." );
  210.  
  211. 			Console.Error.WriteLine( "Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them" );
  212.  
  213. 			Console.Error.WriteLine( "in braces. In other applications, brackets do have a special meaning that might" );
  214.  
  215. 			Console.Error.WriteLine( "be significant when dynamic data exchange (DDE) occurs." );
  216.  
  217. 			Console.Error.WriteLine( );
  218.  
  219. 			Console.Error.Write( "To specify characters that aren't displayed when you press a key, such as " );
  220. 			Console.ForegroundColor = ConsoleColor.White;
  221. 			Console.Error.WriteLine( "ENTER" );
  222. 			Console.ResetColor( );
  223.  
  224. 			Console.Error.Write( "or " );
  225. 			Console.ForegroundColor = ConsoleColor.White;
  226. 			Console.Error.Write( "TAB" );
  227. 			Console.ResetColor( );
  228. 			Console.Error.WriteLine( ", and keys that represent actions rather than characters, use the codes" );
  229.  
  230. 			Console.Error.WriteLine( "in the following table." );
  231.  
  232. 			Console.Error.WriteLine( );
  233.  
  234. 			Console.ForegroundColor = ConsoleColor.White;
  235. 			Console.Error.WriteLine( "Key              Code" );
  236.  
  237. 			Console.Error.WriteLine( "===              ====" );
  238.  
  239. 			Console.Error.WriteLine( "BACKSPACE        {BACKSPACE}, {BS} or {BKSP}" );
  240.  
  241. 			Console.Error.WriteLine( "BREAK            {BREAK}" );
  242.  
  243. 			Console.Error.WriteLine( "CAPS LOCK        {CAPSLOCK}" );
  244.  
  245. 			Console.Error.WriteLine( "DEL or DELETE    {DELETE} or {DEL}" );
  246.  
  247. 			Console.Error.WriteLine( "DOWN ARROW       {DOWN}" );
  248.  
  249. 			Console.Error.WriteLine( "END              {END}" );
  250.  
  251. 			Console.Error.WriteLine( "ENTER            {ENTER} or ~" );
  252.  
  253. 			Console.Error.WriteLine( "ESC              {ESC}" );
  254.  
  255. 			Console.Error.WriteLine( "HELP             {HELP}" );
  256.  
  257. 			Console.Error.WriteLine( "HOME             {HOME}" );
  258.  
  259. 			Console.Error.WriteLine( "INS or INSERT    {INSERT} or {INS}" );
  260.  
  261. 			Console.Error.WriteLine( "LEFT ARROW       {LEFT}" );
  262.  
  263. 			Console.Error.WriteLine( "NUM LOCK         {NUMLOCK}" );
  264.  
  265. 			Console.Error.WriteLine( "PAGE DOWN        {PGDN}" );
  266.  
  267. 			Console.Error.WriteLine( "PAGE UP          {PGUP}" );
  268.  
  269. 			Console.Error.WriteLine( "PRINT SCREEN     {PRTSC} (reserved for future use)" );
  270.  
  271. 			Console.Error.WriteLine( "RIGHT ARROW      {RIGHT}" );
  272.  
  273. 			Console.Error.WriteLine( "SCROLL LOCK      {SCROLLLOCK}" );
  274.  
  275. 			Console.Error.WriteLine( "TAB              {TAB}" );
  276.  
  277. 			Console.Error.WriteLine( "UP ARROW         {UP}" );
  278.  
  279. 			Console.Error.WriteLine( "F1               {F1}" );
  280.  
  281. 			Console.Error.WriteLine( "F2               {F2}" );
  282.  
  283. 			Console.Error.WriteLine( " . . ." );
  284.  
  285. 			Console.Error.WriteLine( "F16              {F16}" );
  286.  
  287. 			Console.Error.WriteLine( "Keypad add       {ADD}" );
  288.  
  289. 			Console.Error.WriteLine( "Keypad subtract  {SUBTRACT}" );
  290.  
  291. 			Console.Error.WriteLine( "Keypad multiply  {MULTIPLY}" );
  292.  
  293. 			Console.Error.WriteLine( "Keypad divide    {DIVIDE}" );
  294.  
  295. 			Console.Error.WriteLine( );
  296.  
  297. 			Console.Error.WriteLine( "To specify keys combined with any combination of the SHIFT, CTRL and" );
  298.  
  299. 			Console.Error.WriteLine( "ALT keys, precede the key code with one or more of the following codes." );
  300. 			Console.ResetColor( );
  301.  
  302. 			Console.Error.WriteLine( );
  303.  
  304. 			Console.ForegroundColor = ConsoleColor.White;
  305. 			Console.Error.WriteLine( "Key      Code" );
  306.  
  307. 			Console.Error.WriteLine( "===      ====" );
  308.  
  309. 			Console.Error.WriteLine( "SHIFT    +" );
  310.  
  311. 			Console.Error.WriteLine( "CTRL     ^" );
  312.  
  313. 			Console.Error.WriteLine( "ALT      %" );
  314. 			Console.ResetColor( );
  315.  
  316. 			Console.Error.WriteLine( );
  317.  
  318. 			Console.Error.Write( "To specify that any combination of " );
  319. 			Console.ForegroundColor = ConsoleColor.White;
  320. 			Console.Error.Write( "SHIFT" );
  321. 			Console.ResetColor( );
  322. 			Console.Error.Write( ", " );
  323. 			Console.ForegroundColor = ConsoleColor.White;
  324. 			Console.Error.Write( "CTRL" );
  325. 			Console.ResetColor( );
  326. 			Console.Error.Write( " and " );
  327. 			Console.ForegroundColor = ConsoleColor.White;
  328. 			Console.Error.Write( "ALT" );
  329. 			Console.ResetColor( );
  330. 			Console.Error.WriteLine( " should be held down while" );
  331.  
  332. 			Console.Error.WriteLine( "several other keys are pressed, enclose the code for those keys in parentheses." );
  333.  
  334. 			Console.Error.Write( "For example, to specify to hold down " );
  335. 			Console.ForegroundColor = ConsoleColor.White;
  336. 			Console.Error.Write( "SHIFT" );
  337. 			Console.ResetColor( );
  338. 			Console.Error.Write( " while " );
  339. 			Console.ForegroundColor = ConsoleColor.White;
  340. 			Console.Error.Write( "E" );
  341. 			Console.ResetColor( );
  342. 			Console.Error.Write( " and " );
  343. 			Console.ForegroundColor = ConsoleColor.White;
  344. 			Console.Error.Write( "C" );
  345. 			Console.ResetColor( );
  346. 			Console.Error.WriteLine( " are pressed, use" );
  347.  
  348. 			Console.Error.Write( "\"" );
  349. 			Console.ForegroundColor = ConsoleColor.White;
  350. 			Console.Error.Write( "+(EC)" );
  351. 			Console.ResetColor( );
  352. 			Console.Error.Write( "\"; to specify to hold down " );
  353. 			Console.ForegroundColor = ConsoleColor.White;
  354. 			Console.Error.Write( "SHIFT" );
  355. 			Console.ResetColor( );
  356. 			Console.Error.Write( " while " );
  357. 			Console.ForegroundColor = ConsoleColor.White;
  358. 			Console.Error.Write( "E" );
  359. 			Console.ResetColor( );
  360. 			Console.Error.Write( " is pressed, followed by " );
  361. 			Console.ForegroundColor = ConsoleColor.White;
  362. 			Console.Error.Write( "C" );
  363. 			Console.ResetColor( );
  364. 			Console.Error.WriteLine( " without" );
  365.  
  366. 			Console.ForegroundColor = ConsoleColor.White;
  367. 			Console.Error.Write( "SHIFT" );
  368. 			Console.ResetColor( );
  369. 			Console.Error.Write( ", use \"" );
  370. 			Console.ForegroundColor = ConsoleColor.White;
  371. 			Console.Error.Write( "+EC" );
  372. 			Console.ResetColor( );
  373. 			Console.Error.WriteLine( "\"." );
  374.  
  375. 			Console.Error.WriteLine( );
  376.  
  377. 			Console.Error.Write( "To specify repeating keys, use the form " );
  378. 			Console.ForegroundColor = ConsoleColor.White;
  379. 			Console.Error.Write( "{key number}" );
  380. 			Console.ResetColor( );
  381. 			Console.Error.WriteLine( ". You must put a space between" );
  382.  
  383. 			Console.ForegroundColor = ConsoleColor.White;
  384. 			Console.Error.Write( "key" );
  385. 			Console.ResetColor( );
  386. 			Console.Error.Write( " and " );
  387. 			Console.ForegroundColor = ConsoleColor.White;
  388. 			Console.Error.Write( "number" );
  389. 			Console.ResetColor( );
  390. 			Console.Error.WriteLine( "." );
  391.  
  392. 			Console.Error.Write( "E.g. " );
  393. 			Console.ForegroundColor = ConsoleColor.White;
  394. 			Console.Error.Write( "{UP 42}" );
  395. 			Console.ResetColor( );
  396. 			Console.Error.Write( " means press the " );
  397. 			Console.ForegroundColor = ConsoleColor.White;
  398. 			Console.Error.Write( "UP ARROW" );
  399. 			Console.ResetColor( );
  400. 			Console.Error.Write( " key " );
  401. 			Console.ForegroundColor = ConsoleColor.White;
  402. 			Console.Error.Write( "42" );
  403. 			Console.ResetColor( );
  404. 			Console.Error.Write( " times; " );
  405. 			Console.ForegroundColor = ConsoleColor.White;
  406. 			Console.Error.Write( "{h 10}" );
  407. 			Console.ResetColor( );
  408. 			Console.Error.Write( " means press " );
  409. 			Console.ForegroundColor = ConsoleColor.White;
  410. 			Console.Error.Write( "H 10" );
  411. 			Console.ResetColor( );
  412. 			Console.Error.WriteLine( " times." );
  413.  
  414. 			Console.Error.WriteLine( );
  415.  
  416. 			Console.Error.Write( "Source: " );
  417. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  418. 			Console.Error.WriteLine( "https://docs.microsoft.com/dotnet/api/system.windows.forms.sendkeys.send" );
  419. 			Console.ResetColor( );
  420.  
  421. 			return -1;
  422. 		}
  423.  
  424.  
  425. 		#endregion Error handling
  426. 	}
  427. }
  428.  

page last uploaded: 2019-01-21, 22:48