Checking Num Lock Status
The Num Lock status determines whether the numeric keypad on a keyboard can be used to enter numbers or is set to control the cursor position. On notebook computers, Num Lock may alternate between numbers and letters.
The Console class, within the System namespace, represents the standard input, output and error streams for console applications. Previously I have explained how to use this class to determine the Caps Lock status. In response to requests, this tip explains how to test for the status of the Num Lock key.
When using the .NET framework 2.0 and later, the Console class definition includes a property named NumberLock. This Boolean property returns true if Num Lock is switched on, and false if it is not. If you wish to determine the status of the Num Lock key you can simply check the property's value, as in the sample code below:
if (Console.NumberLock) Console.WriteLine("Num Lock is on!");
As with checking the Caps Lock key, you can use the Control class from the System.Windows.Forms namespace to check the status of the Num Lock key. This requires the use of the IsKeyLocked method.
if (Control.IsKeyLocked(Keys.NumLock)) MessageBox.Show("Num Lock is on!");
10 August 2008