Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for keylocks.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class KeyLocks
  10. 	{
  11. 		static string progver = "1.01";
  12.  
  13. 		#region Global Variables
  14.  
  15. 		static string keylocks = String.Empty;
  16. 		static int keystatus = -1;
  17. 		static bool capslock = Control.IsKeyLocked( Keys.CapsLock );
  18. 		static bool numlock = Control.IsKeyLocked( Keys.NumLock );
  19. 		static bool scrolllock = Control.IsKeyLocked( Keys.Scroll );
  20. 		static bool insert = Control.IsKeyLocked( Keys.Insert );
  21. 		static bool returncapslockonly = false;
  22. 		static bool returnnumlockonly = false;
  23. 		static bool returnscrolllockonly = false;
  24. 		static bool returninsertonly = false;
  25.  
  26. 		#endregion Global Variables
  27.  
  28.  
  29. 		static int Main( string[] args )
  30. 		{
  31. 			int interval = 1;
  32. 			bool loop = false;
  33. 			bool returnansi = false;
  34. 			bool settitle = false;
  35.  
  36. 			#region Parse Command Line
  37.  
  38. 			foreach ( string arg in args )
  39. 			{
  40. 				if ( arg[0] == '/' && arg.Length > 1 )
  41. 				{
  42. 					switch ( arg[1] )
  43. 					{
  44. 						case '?':
  45. 							return ShowHelp( );
  46. 						case 'a':
  47. 						case 'A':
  48. 							if ( returnansi )
  49. 							{
  50. 								return ShowHelp( "Duplicate command line switch /A" );
  51. 							}
  52. 							returnansi = true;
  53. 							break;
  54. 						case 'c':
  55. 						case 'C':
  56. 							if ( returncapslockonly )
  57. 							{
  58. 								return ShowHelp( "Duplicate command line switch /C" );
  59. 							}
  60. 							returncapslockonly = true;
  61. 							break;
  62. 						case 'i':
  63. 						case 'I':
  64. 							if ( returninsertonly )
  65. 							{
  66. 								return ShowHelp( "Duplicate command line switch /I" );
  67. 							}
  68. 							returninsertonly = true;
  69. 							break;
  70. 						case 'l':
  71. 						case 'L':
  72. 							if ( loop )
  73. 							{
  74. 								return ShowHelp( "Duplicate command line switch /L" );
  75. 							}
  76. 							loop = true;
  77. 							if ( arg.Length > 3 && arg[2] == ':' )
  78. 							{
  79. 								if ( !Int32.TryParse( arg.Substring( 3 ), out interval ) )
  80. 								{
  81. 									return ShowHelp( "Invalid loop interval " + arg );
  82. 								}
  83. 								if ( interval < 1 || interval > 10 )
  84. 								{
  85. 									return ShowHelp( "Loop interval " + interval + " outside allowed range (1..10)" );
  86. 								}
  87. 							}
  88. 							break;
  89. 						case 'n':
  90. 						case 'N':
  91. 							if ( returnnumlockonly )
  92. 							{
  93. 								return ShowHelp( "Duplicate command line switch /I" );
  94. 							}
  95. 							returnnumlockonly = true;
  96. 							break;
  97. 						case 's':
  98. 						case 'S':
  99. 							if ( returnscrolllockonly )
  100. 							{
  101. 								return ShowHelp( "Duplicate command line switch /S" );
  102. 							}
  103. 							returnscrolllockonly = true;
  104. 							break;
  105. 						case 't':
  106. 						case 'T':
  107. 							if ( settitle )
  108. 							{
  109. 								return ShowHelp( "Duplicate command line switch /S" );
  110. 							}
  111. 							settitle = true;
  112. 							break;
  113. 						default:
  114. 							return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  115. 					}
  116. 				}
  117. 				else
  118. 				{
  119. 					return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  120. 				}
  121. 			}
  122.  
  123. 			#endregion Parse Command Line
  124.  
  125. 			if ( loop )
  126. 			{
  127. 				while ( true )
  128. 				{
  129. 					GetStatus( );
  130. 					if ( settitle )
  131. 					{
  132. 						SetTitle( );
  133. 					}
  134. 					Thread.Sleep( interval * 1000 );
  135. 				}
  136. 			}
  137. 			else
  138. 			{
  139. 				GetStatus( );
  140. 				if ( settitle )
  141. 				{
  142. 					SetTitle( );
  143. 				}
  144. 				if ( returnansi )
  145. 				{
  146. 					#region Build ANSI String
  147.  
  148. 					int statusx = Console.BufferWidth - 4;
  149. 					int cursorx = Console.CursorLeft;
  150. 					int cursory = Console.CursorTop;
  151. 					string cursorsave = "\x1B[s";
  152. 					string cursorrestore = "\x1B[u";
  153. 					string clearline = "\x1B[K";
  154. 					string cursormove = "\x1B[1D ";
  155. 					string boldgreen = "\x1B[1;32m";
  156. 					string resetcolors = "\x1B[0m";
  157. 					bool bold = false;
  158. 					if ( cursorx < statusx )
  159. 					{
  160. 						cursormove = String.Format( "\x1B[{0}C", statusx - cursorx );
  161. 					}
  162. 					else if ( cursorx > statusx )
  163. 					{
  164. 						cursormove = String.Format( "\x1B[{0}D ", 1 + cursorx - statusx );
  165. 					}
  166. 					string status = String.Empty;
  167. 					if ( capslock )
  168. 					{
  169. 						bold = true;
  170. 						status += boldgreen + "C";
  171. 					}
  172. 					else
  173. 					{
  174. 						bold = false;
  175. 						status = "c";
  176. 					}
  177. 					if ( numlock )
  178. 					{
  179. 						if ( !bold )
  180. 						{
  181. 							bold = true;
  182. 							status += boldgreen;
  183. 						}
  184. 						status += "N";
  185. 					}
  186. 					else
  187. 					{
  188. 						if ( bold )
  189. 						{
  190. 							status += resetcolors;
  191. 							bold = false;
  192. 						}
  193. 						status += "n";
  194. 					}
  195. 					if ( scrolllock )
  196. 					{
  197. 						if ( !bold )
  198. 						{
  199. 							bold = true;
  200. 							status += boldgreen;
  201. 						}
  202. 						status += "S";
  203. 					}
  204. 					else
  205. 					{
  206. 						if ( bold )
  207. 						{
  208. 							status += resetcolors;
  209. 							bold = false;
  210. 						}
  211. 						status += "s";
  212. 					}
  213. 					if ( insert )
  214. 					{
  215. 						if ( !bold )
  216. 						{
  217. 							bold = true;
  218. 							status += boldgreen;
  219. 						}
  220. 						status += "I";
  221. 					}
  222. 					else
  223. 					{
  224. 						if ( bold )
  225. 						{
  226. 							status += resetcolors;
  227. 							bold = false;
  228. 						}
  229. 						status += "i";
  230. 					}
  231. 					if ( bold )
  232. 					{
  233. 						bold = false;
  234. 						status += resetcolors;
  235. 					}
  236.  
  237. 					#endregion Build ANSI String
  238.  
  239. 					Console.Write( "{0}{1}{2}{3}{4}", cursorsave, clearline, cursormove, status, cursorrestore );
  240. 				}
  241. 				else
  242. 				{
  243. 					Console.WriteLine( keylocks );
  244. 				}
  245. 			}
  246.  
  247. 			return keystatus;
  248. 		}
  249.  
  250.  
  251. 		static void GetStatus( )
  252. 		{
  253. 			capslock = Control.IsKeyLocked( Keys.CapsLock );
  254. 			numlock = Control.IsKeyLocked( Keys.NumLock );
  255. 			scrolllock = Control.IsKeyLocked( Keys.Scroll );
  256. 			insert = Control.IsKeyLocked( Keys.Insert );
  257. 			if ( returncapslockonly )
  258. 			{
  259. 				keystatus = ( capslock ? 1 : 0 );
  260. 			}
  261. 			else if ( returninsertonly )
  262. 			{
  263. 				keystatus = ( insert ? 1 : 0 );
  264. 			}
  265. 			else if ( returnnumlockonly )
  266. 			{
  267. 				keystatus = ( numlock ? 1 : 0 );
  268. 			}
  269. 			else if ( returnscrolllockonly )
  270. 			{
  271. 				keystatus = ( scrolllock ? 1 : 0 );
  272. 			}
  273. 			else
  274. 			{
  275. 				keystatus = ( capslock ? 1 : 0 ) + ( numlock ? 2 : 0 ) + ( scrolllock ? 4 : 0 ) + ( insert ? 8 : 0 );
  276. 			}
  277. 			keylocks = ( capslock ? "C" : "c" ) + ( numlock ? "N" : "n" ) + ( scrolllock ? "S" : "s" ) + ( insert ? "I" : "i" );
  278. 		}
  279.  
  280.  
  281. 		static void SetTitle( )
  282. 		{
  283. 			string title = Console.Title;
  284. 			Console.Title = ( title + new String( ' ', 100 ) ).Substring( 0, 100 ) + keylocks;
  285. 		}
  286.  
  287.  
  288. 		static int ShowHelp( params string[] errmsg )
  289.  
  290. 		{
  291.  
  292. 			#region Help Text
  293.  
  294. 			/*
  295. 			KeyLocks.exe,  Version 1.01
  296. 			Return status for CapsLock, NumLock, ScrollLock and Insert keys
  297.  
  298. 			Usage:  KEYLOCKS   [ /A ]  [ /C | /I | /N | /S ]  [ /L[:sec] ]  [ /T ]
  299.  
  300. 			Where:  /A         generate ANSI sequence of status
  301. 			        /C         return code 1 if CapsLock is on, otherwise 0
  302. 			        /I         return code 1 if Insert is on, otherwise 0
  303. 			        /L[:sec]   continuous Loop with interval in seconds (1..10; default: 1)
  304. 			        /N         return code 1 if NumLock is on, otherwise 0
  305. 			        /S         return code 1 if ScrollLock is on, otherwise 0
  306. 			        /T         show status in window Title
  307.  
  308. 			Notes:  /L excludes all other switches except /T.
  309. 			        /T is of little use without /L as the title is restored to its
  310. 			        previous state (without status) as soon as this program terminates.
  311. 			        Switches /C, /I, /N and /S are all mutually exclusive.
  312. 					With /T status is shown in the console window title bar as CNSI where
  313. 			        C is for CapsLock, N for NumLock, etcetera; a capital character means
  314. 			        the key lock is on, a lower case character means it is off.
  315. 			        In case of (command line) errors, the return code ("errorlevel") is -1.
  316. 			        With /C, /I, /N and /S retun code is 0 for key lock off or 1 if on.
  317. 			        With /L return code is 0.
  318. 			        Otherwise the returncode represents the key locks status:
  319. 			        0 if all key locks are off, +1 if capsLock is on,
  320. 			        +2 if NumLock is on, +4 if ScrollLock is on, +8 if Insert is on.
  321.  
  322. 			Written by Rob van der Woude
  323. 			https://www.robvanderwoude.com
  324. 			*/
  325.  
  326. 			#endregion Help Text
  327.  
  328.  
  329. 			#region Error Message
  330.  
  331. 			if ( errmsg.Length > 0 )
  332. 			{
  333. 				List<string> errargs = new List<string>( errmsg );
  334. 				errargs.RemoveAt( 0 );
  335. 				Console.Error.WriteLine( );
  336. 				Console.ForegroundColor = ConsoleColor.Red;
  337. 				Console.Error.Write( "ERROR:\t" );
  338. 				Console.ForegroundColor = ConsoleColor.White;
  339. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  340. 				Console.ResetColor( );
  341. 			}
  342.  
  343. 			#endregion Error Message
  344.  
  345.  
  346. 			#region Display Help Text
  347.  
  348. 			Console.Error.WriteLine( );
  349.  
  350. 			Console.Error.WriteLine( "KeyLocks.exe,  Version {0}", progver );
  351.  
  352. 			Console.Error.WriteLine( "Return status for CapsLock, NumLock, ScrollLock and Insert keys" );
  353.  
  354. 			Console.Error.WriteLine( );
  355.  
  356. 			Console.Error.Write( "Usage:  " );
  357. 			Console.ForegroundColor = ConsoleColor.White;
  358. 			Console.Error.WriteLine( "KEYLOCKS   [ /A ]  [ /C | /I | /N | /S ]  [ /L[:sec] ]  [ /T ]" );
  359. 			Console.ResetColor( );
  360.  
  361. 			Console.Error.WriteLine( );
  362.  
  363. 			Console.Error.Write( "Where:  " );
  364. 			Console.ForegroundColor = ConsoleColor.White;
  365. 			Console.Error.Write( "/A" );
  366. 			Console.ResetColor( );
  367. 			Console.Error.Write( "         generate " );
  368. 			Console.ForegroundColor = ConsoleColor.White;
  369. 			Console.Error.Write( "A" );
  370. 			Console.ResetColor( );
  371. 			Console.Error.WriteLine( "NSI sequence of status" );
  372.  
  373. 			Console.ForegroundColor = ConsoleColor.White;
  374. 			Console.Error.Write( "        /C" );
  375. 			Console.ResetColor( );
  376. 			Console.Error.Write( "         return code 1 if " );
  377. 			Console.ForegroundColor = ConsoleColor.White;
  378. 			Console.Error.Write( "C" );
  379. 			Console.ResetColor( );
  380. 			Console.Error.WriteLine( "apsLock is on, otherwise 0" );
  381.  
  382. 			Console.ForegroundColor = ConsoleColor.White;
  383. 			Console.Error.Write( "        /I" );
  384. 			Console.ResetColor( );
  385. 			Console.Error.Write( "         return code 1 if " );
  386. 			Console.ForegroundColor = ConsoleColor.White;
  387. 			Console.Error.Write( "I" );
  388. 			Console.ResetColor( );
  389. 			Console.Error.WriteLine( "nsert is on, otherwise 0" );
  390.  
  391. 			Console.ForegroundColor = ConsoleColor.White;
  392. 			Console.Error.Write( "        /L[:sec]" );
  393. 			Console.ResetColor( );
  394. 			Console.Error.Write( "   continuous " );
  395. 			Console.ForegroundColor = ConsoleColor.White;
  396. 			Console.Error.Write( "L" );
  397. 			Console.ResetColor( );
  398. 			Console.Error.Write( "oop with interval in " );
  399. 			Console.ForegroundColor = ConsoleColor.White;
  400. 			Console.Error.Write( "sec" );
  401. 			Console.ResetColor( );
  402. 			Console.Error.WriteLine( "onds (1..10; default: 1)" );
  403.  
  404. 			Console.ForegroundColor = ConsoleColor.White;
  405. 			Console.Error.Write( "        /N" );
  406. 			Console.ResetColor( );
  407. 			Console.Error.Write( "         return code 1 if " );
  408. 			Console.ForegroundColor = ConsoleColor.White;
  409. 			Console.Error.Write( "N" );
  410. 			Console.ResetColor( );
  411. 			Console.Error.WriteLine( "umLock is on, otherwise 0" );
  412.  
  413. 			Console.ForegroundColor = ConsoleColor.White;
  414. 			Console.Error.Write( "        /S" );
  415. 			Console.ResetColor( );
  416. 			Console.Error.Write( "         return code 1 if " );
  417. 			Console.ForegroundColor = ConsoleColor.White;
  418. 			Console.Error.Write( "S" );
  419. 			Console.ResetColor( );
  420. 			Console.Error.WriteLine( "crollLock is on, otherwise 0" );
  421.  
  422. 			Console.ForegroundColor = ConsoleColor.White;
  423. 			Console.Error.Write( "        /T" );
  424. 			Console.ResetColor( );
  425. 			Console.Error.Write( "         show status in window " );
  426. 			Console.ForegroundColor = ConsoleColor.White;
  427. 			Console.Error.Write( "T" );
  428. 			Console.ResetColor( );
  429. 			Console.Error.WriteLine( "itle" );
  430.  
  431. 			Console.Error.WriteLine( );
  432.  
  433. 			Console.Error.Write( "Notes:  " );
  434. 			Console.ForegroundColor = ConsoleColor.White;
  435. 			Console.Error.Write( "/L" );
  436. 			Console.ResetColor( );
  437. 			Console.Error.Write( " excludes all other switches except " );
  438. 			Console.ForegroundColor = ConsoleColor.White;
  439. 			Console.Error.Write( "/T" );
  440. 			Console.ResetColor( );
  441. 			Console.Error.WriteLine( "." );
  442.  
  443. 			Console.ForegroundColor = ConsoleColor.White;
  444. 			Console.Error.Write( "        /T" );
  445. 			Console.ResetColor( );
  446. 			Console.Error.Write( " is of little use without " );
  447. 			Console.ForegroundColor = ConsoleColor.White;
  448. 			Console.Error.Write( "/L" );
  449. 			Console.ResetColor( );
  450. 			Console.Error.WriteLine( " as the title is restored to its" );
  451.  
  452. 			Console.Error.WriteLine( "        previous state (without status) as soon as this program terminates." );
  453.  
  454. 			Console.Error.Write( "        Switches " );
  455. 			Console.ForegroundColor = ConsoleColor.White;
  456. 			Console.Error.Write( "/C" );
  457. 			Console.ResetColor( );
  458. 			Console.Error.Write( ", " );
  459. 			Console.ForegroundColor = ConsoleColor.White;
  460. 			Console.Error.Write( "/I" );
  461. 			Console.ResetColor( );
  462. 			Console.Error.Write( ", " );
  463. 			Console.ForegroundColor = ConsoleColor.White;
  464. 			Console.Error.Write( "/N" );
  465. 			Console.ResetColor( );
  466. 			Console.Error.Write( " and " );
  467. 			Console.ForegroundColor = ConsoleColor.White;
  468. 			Console.Error.Write( "/S" );
  469. 			Console.ResetColor( );
  470. 			Console.Error.WriteLine( " are all mutually exclusive." );
  471.  
  472. 			Console.Error.Write( "        With " );
  473. 			Console.ForegroundColor = ConsoleColor.White;
  474. 			Console.Error.Write( "/T" );
  475. 			Console.ResetColor( );
  476. 			Console.Error.Write( " status is shown in the console window title bar as " );
  477. 			Console.ForegroundColor = ConsoleColor.White;
  478. 			Console.Error.Write( "CNSI" );
  479. 			Console.ResetColor( );
  480. 			Console.Error.WriteLine( " where" );
  481.  
  482. 			Console.ForegroundColor = ConsoleColor.White;
  483. 			Console.Error.Write( "        C" );
  484. 			Console.ResetColor( );
  485. 			Console.Error.Write( " is for " );
  486. 			Console.ForegroundColor = ConsoleColor.White;
  487. 			Console.Error.Write( "C" );
  488. 			Console.ResetColor( );
  489. 			Console.Error.Write( "apsLock, " );
  490. 			Console.ForegroundColor = ConsoleColor.White;
  491. 			Console.Error.Write( "N" );
  492. 			Console.ResetColor( );
  493. 			Console.Error.Write( " for " );
  494. 			Console.ForegroundColor = ConsoleColor.White;
  495. 			Console.Error.Write( "N" );
  496. 			Console.ResetColor( );
  497. 			Console.Error.WriteLine( "umLock, etcetera; a capital character means" );
  498.  
  499. 			Console.Error.WriteLine( "        the key lock is on, a lower case character means it is off." );
  500.  
  501. 			Console.Error.WriteLine( "        In case of (command line) errors, the return code (\"errorlevel\") is -1." );
  502.  
  503. 			Console.Error.Write( "        With " );
  504. 			Console.ForegroundColor = ConsoleColor.White;
  505. 			Console.Error.Write( "/C" );
  506. 			Console.ResetColor( );
  507. 			Console.Error.Write( ", " );
  508. 			Console.ForegroundColor = ConsoleColor.White;
  509. 			Console.Error.Write( "/I" );
  510. 			Console.ResetColor( );
  511. 			Console.Error.Write( ", " );
  512. 			Console.ForegroundColor = ConsoleColor.White;
  513. 			Console.Error.Write( "/N" );
  514. 			Console.ResetColor( );
  515. 			Console.Error.Write( " and " );
  516. 			Console.ForegroundColor = ConsoleColor.White;
  517. 			Console.Error.Write( "/S" );
  518. 			Console.ResetColor( );
  519. 			Console.Error.WriteLine( " retun code is 0 for key lock off or 1 if on." );
  520.  
  521. 			Console.Error.Write( "        With " );
  522. 			Console.ForegroundColor = ConsoleColor.White;
  523. 			Console.Error.Write( "/L" );
  524. 			Console.ResetColor( );
  525. 			Console.Error.WriteLine( " return code is 0." );
  526.  
  527. 			Console.Error.WriteLine( "        Otherwise the returncode represents the key locks status:" );
  528.  
  529. 			Console.Error.WriteLine( "        0 if all key locks are off, +1 if capsLock is on," );
  530.  
  531. 			Console.Error.WriteLine( "        +2 if NumLock is on, +4 if ScrollLock is on, +8 if Insert is on." );
  532.  
  533. 			Console.Error.WriteLine( );
  534.  
  535. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  536.  
  537. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  538.  
  539. 			#endregion Display Help Text
  540.  
  541.  
  542. 			return -1;
  543. 		}
  544. 	}
  545. }

page last modified: 2024-04-16; loaded in 0.0138 seconds