Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for numlock.cs

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

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	public class NumLock
  8. 	{
  9. 		[DllImport( "user32.dll" )]
  10. 		static extern void keybd_event( byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo );
  11. 		const int KEYEVENTF_EXTENDEDKEY = 0x1;
  12. 		const int KEYEVENTF_KEYUP = 0x2;
  13.  
  14. 		public static int Main( string[] args )
  15. 		{
  16. 			try
  17. 			{
  18. 				bool numLock;
  19. 				bool newState = false;
  20. 				bool setState = false;
  21. 				bool setVerbose = false;
  22. 				bool verbose = true;
  23.  
  24. 				if ( args.Length > 2 )
  25. 				{
  26. 					return WriteError( "Invalid command line argument(s)." );
  27. 				}
  28. 				else
  29. 				{
  30. 					foreach ( string arg in args )
  31. 					{
  32. 						switch ( arg.ToUpper( ) )
  33. 						{
  34. 							case "/?":
  35. 							case "-H":
  36. 							case "/HELP":
  37. 							case "-HELP":
  38. 							case "--HELP":
  39. 								return WriteError( string.Empty );
  40. 							case "/Q":
  41. 							case "-Q":
  42. 							case "/QUIET":
  43. 							case "-QUIET":
  44. 							case "--QUIET":
  45. 								if ( setVerbose )
  46. 								{
  47. 									return WriteError( "Duplicate switch not allowed." );
  48. 								}
  49. 								setVerbose = true;
  50. 								verbose = false;
  51. 								break;
  52. 							case "/V":
  53. 							case "-V":
  54. 							case "/VERBOSE":
  55. 							case "-VERBOSE":
  56. 							case "--VERBOSE":
  57. 								if ( setVerbose )
  58. 								{
  59. 									return WriteError( "Duplicate switch not allowed." );
  60. 								}
  61. 								setVerbose = true;
  62. 								verbose = true;
  63. 								break;
  64. 							case "0":
  65. 							case "OFF":
  66. 								if ( setState )
  67. 								{
  68. 									return WriteError( "Duplicate argument not allowed." );
  69. 								}
  70. 								setState = true;
  71. 								newState = false;
  72. 								break;
  73. 							case "1":
  74. 							case "ON":
  75. 								if ( setState )
  76. 								{
  77. 									return WriteError( "Duplicate argument not allowed." );
  78. 								}
  79. 								setState = true;
  80. 								newState = true;
  81. 								break;
  82. 							default:
  83. 								return WriteError( "Invalid command line argument " + arg );
  84. 						}
  85. 					}
  86. 				}
  87.  
  88. 				if ( Control.IsKeyLocked( Keys.NumLock ) )
  89. 				{
  90. 					numLock = true;
  91. 					if ( setState && !newState )
  92. 					{
  93. 						keybd_event( 0x90, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0 );
  94. 						keybd_event( 0x90, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr) 0 );
  95. 					}
  96. 				}
  97. 				else
  98. 				{
  99. 					numLock = false;
  100. 					if ( setState && newState )
  101. 					{
  102. 						keybd_event( 0x90, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 1 );
  103. 						keybd_event( 0x90, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr) 1 );
  104. 					}
  105. 				}
  106.  
  107. 				numLock = Control.IsKeyLocked( Keys.NumLock );
  108. 				if ( verbose )
  109. 				{
  110. 					Console.Write( "NumLock = " );
  111. 					Console.ForegroundColor = ConsoleColor.White;
  112. 					Console.WriteLine( ( numLock ? "ON" : "OFF" ) );
  113. 					Console.ResetColor( );
  114. 				}
  115. 				return ( numLock ? 2 : 0 );
  116. 			}
  117. 			catch ( Exception e )
  118. 			{
  119. 				return WriteError( e );
  120. 			}
  121. 		}
  122.  
  123. 		public static int WriteError( Exception e )
  124. 		{
  125. 			return WriteError( e == null ? null : e.Message );
  126. 		}
  127.  
  128. 		public static int WriteError( string errorMessage )
  129. 		{
  130. 			/*
  131. 			NumLock,  Version 1.01
  132. 			Read or set NumLock key state
  133.  
  134. 			Usage:  NUMLOCK  [ 0 | 1 | ON | OFF ]  [ /Quiet | /Verbose ]
  135.  
  136. 			Where:  0 or OFF   sets NumLock off
  137. 					1 or ON    sets NumLock on
  138. 			        /Quiet     won't display anything on screen
  139. 			        /Verbose   displays the (new) NumLock key state on screen (default)
  140.  
  141. 			Notes:  An \"errorlevel\" 0 is returned if NumLock is off, 2 if
  142. 					NumLock is on, or 1 in case of (command line) errors.
  143.  
  144. 			Written by Rob van der Woude
  145. 			http://www.robvanderwoude.com
  146. 			*/
  147.  
  148. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  149. 			{
  150. 				Console.Error.WriteLine( );
  151. 				Console.ForegroundColor = ConsoleColor.Red;
  152. 				Console.Error.Write( "ERROR:  " );
  153. 				Console.ForegroundColor = ConsoleColor.White;
  154. 				Console.Error.WriteLine( errorMessage );
  155. 				Console.ResetColor( );
  156. 			}
  157. 			Console.Error.WriteLine( );
  158. 			Console.Error.WriteLine( "NumLock,  Version 1.01" );
  159. 			Console.Error.WriteLine( "Read or set NumLock key state" );
  160. 			Console.Error.WriteLine( );
  161. 			Console.Error.Write( "Usage:  " );
  162. 			Console.ForegroundColor = ConsoleColor.White;
  163. 			Console.Error.WriteLine( "NUMLOCK  [ 0 | 1 | ON | OFF ]  [ /Quiet | /Verbose ]" );
  164. 			Console.ResetColor( );
  165. 			Console.Error.WriteLine( );
  166. 			Console.Error.Write( "Where:  " );
  167. 			Console.ForegroundColor = ConsoleColor.White;
  168. 			Console.Error.Write( "0" );
  169. 			Console.ResetColor( );
  170. 			Console.Error.Write( " or " );
  171. 			Console.ForegroundColor = ConsoleColor.White;
  172. 			Console.Error.Write( "OFF" );
  173. 			Console.ResetColor( );
  174. 			Console.Error.WriteLine( "   sets NumLock off" );
  175. 			Console.ForegroundColor = ConsoleColor.White;
  176. 			Console.Error.Write( "        1" );
  177. 			Console.ResetColor( );
  178. 			Console.Error.Write( " or " );
  179. 			Console.ForegroundColor = ConsoleColor.White;
  180. 			Console.Error.Write( "ON" );
  181. 			Console.ResetColor( );
  182. 			Console.Error.WriteLine( "    sets NumLock on" );
  183. 			Console.ForegroundColor = ConsoleColor.White;
  184. 			Console.Error.Write( "        /Q" );
  185. 			Console.ResetColor( );
  186. 			Console.Error.WriteLine( "uiet     won't display anything on screen" );
  187. 			Console.ForegroundColor = ConsoleColor.White;
  188. 			Console.Error.Write( "        /V" );
  189. 			Console.ResetColor( );
  190. 			Console.Error.WriteLine( "erbose   displays the (new) NumLock key state on screen (default)" );
  191. 			Console.Error.WriteLine( );
  192. 			Console.Error.WriteLine( "Notes:  An \"errorlevel\" 0 is returned if NumLock is off, 2 if" );
  193. 			Console.Error.WriteLine( "        NumLock is on, or 1 in case of (command line) errors." );
  194. 			Console.Error.WriteLine( );
  195. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  196. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  197. 			return 1;
  198. 		}
  199. 	}
  200. }

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