Tuesday, March 19, 2013

Checking Num, Caps and Scroll Lock Status C#

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.

Console Class

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 (Control.NumberLock) Console.WriteLine("Num Lock is on!");

Control Class

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.

if (Control.IsKeyLocked(Keys.NumLock)) MessageBox.Show("Num Lock is on!");
 

Checking Scroll Lock Status


The Scroll Lock key is a toggle key that behaves in a similar manner to Caps Lock and Num Lock. The principle purpose for Scroll Lock is to fix the position of the caret, or text cursor, and instead scroll the window when the arrow keys are pressed.

Control Class

To check the status of the key, use the IsKeyLocked method, passing the details of the scroll lock key as follows:

if (Control.IsKeyLocked(Keys.Scroll))
{
    MessageBox.Show("Scroll Lock is on!");
}


Checking Caps Lock Status

Textboxes can be set to hide secret information, such as passwords. If such a textbox receives the focus whilst Caps Lock is on a warning is displayed. There are other situations where you may want to check the Caps Lock status.

Console Class

if (Control.CapsLock) Console.WriteLine("Caps Lock is on!");


Control Class


The Control class is a standard class within the System.Windows.Forms namespace. The DLL containing this namespace is automatically included in Windows Forms applications. The class includes a method named IsKeyLocked, which allows you to determine whether keys such as Caps Lock are switched on or off. To check the status of the Caps Lock key, you can use the method in the following manner:

if (Control.IsKeyLocked(Keys.CapsLock)) MessageBox.Show("Caps Lock is on!");

1 comment: