BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

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. This tip explains how.

Console Class

The Console class is a standard class within the System namespace. It represents the standard input, output and error streams for console applications but is available to other types of project too.

When using the .NET framework 2.0 and later, the Console class definition includes a property named CapsLock. This property returns a Boolean value representing the state of the Caps Lock key on the keyboard. If the property value is true, Caps Lock is enabled. If it is false, Caps Lock is switched off.

If you wish to determine the status of the Caps Lock key using the Console class, you can simply check the property's value, as in the sample code below:

if (Console.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!");
27 July 2008