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 1.1+

Toggling Lockable Key Statuses

The Scroll Lock, Caps Lock and Num Lock keys are three keys on a standard keyboard that have a status that can be either on or off. Using a call to a Windows API function, it is possible to toggle the status of these keys from within a .NET application.

keybd_event Function

In previous articles we have used the Console and Control classes to obtain the current state of the Caps Lock, Num Lock and Scroll Lock keys when using the .NET framework version 2.0 or later. For users of earlier versions of the .NET framework, we have also investigated how we can obtain the status of lockable keys using Windows API calls. In this article I will expand upon this by describing how the status of the key can be toggled from code, again using the Windows API and Platform Invocation Services (P/Invoke). By combining this knowledge with that of the earlier articles, it will also be possible for you to specify the state that you require for each of the three keys.

Toggling the locked state of the Caps Lock, Num Lock and Scroll Lock keys is achieved using the keybd_event function from the user32.dll library. This function allows you to simulate keyboard events such as pressing and releasing a key. The function accepts four parameters, although only two are required for toggling keys. These are:

  • bVk. Specifies a virtual key code. This represents the key that is to be controlled by the function.
  • dwFlags. Accepts flags that determine the action that will be performed. These flags can be used to specify whether the key is being pressed down or released.

To declare the function and the flags that will be used in this article, add the following code to your class:

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, IntPtr dwExtraInfo);

private const uint KEYEVENTF_EXTENDEDKEY = 0x01;
private const uint KEYEVENTF_KEYUP = 0x02;

The DllImport attribute is found in the System.Runtime.InteropServices namespace. To allow the above to operate correctly, also add the following directive:

using System.Runtime.InteropServices;

To complete the declarations that we will require, add the three constants below. These represent the three virtual key codes for the Caps Lock, Num Lock and Scroll Lock keys:

private const byte VK_CAPITAL = 0x14;
private const byte VK_NUMLOCK = 0x90;
private const byte VK_SCROLL = 0x91;

Toggling Key States

With the required declarations in place we can toggle the state of one of the lockable keys by making two calls to the API function. The first call simulates depressing the key, the second call releasing it. You should always make both calls to ensure that the keyboard state remains valid. If you do not, you may find that a second call to the function does not revert the key to its original state. You may also find that the next time that the user presses the key, it behaves incorrectly.

To simulate depressing the key, call the function with the appropriate virtual key code and the KEYEVENTF_EXTENDEDKEY flag. The other, unused parameters should be set to zero. To release the key, combine the KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP flags using the OR logical bitwise operator. For example, to toggle all three keys, execute the following code:

uint keyDown = KEYEVENTF_EXTENDEDKEY;
uint keyUp = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP;

keybd_event(VK_NUMLOCK, (byte)0, keyDown, new IntPtr(0));
keybd_event(VK_NUMLOCK, (byte)0, keyUp, new IntPtr(0));

keybd_event(VK_CAPITAL, (byte)0, keyDown, new IntPtr(0));
keybd_event(VK_CAPITAL, (byte)0, keyUp, new IntPtr(0));

keybd_event(VK_SCROLL, (byte)0, keyDown, new IntPtr(0));
keybd_event(VK_SCROLL, (byte)0, keyUp, new IntPtr(0));
24 May 2009