Introduction

A few months ago, I was developing a desktop application for a client. After completing the requirements, I was enhancing the UI specially the main form. In the previous versions of Microsoft Word (before version 2007), there are some labels which are clickable and reflect the changes to the keyboard (details below) and I wanted to add these features to my applications.

HTML documentation of the code has also been included along the source in the zip file. Select the index.html in the HTML folder to view it.

Toggle Keys

There are three LEDs on almost every keyboard, i.e. NUM lock, CAPS lock and SCROLL lock. These LEDS shows the status of the respective keys. As these keys are either in ON state or OFF state, their value is toggled between 1 and 0, so these are called Toggle Keys. The INSERT key is also included in toggle keys, although there is no LED for it but its value is either ON (insert mode) or OFF (overwrite mode), so it is also included in this category.

In my application, I wanted to add the status of NUM lock and CAPS lock. I Googled it and got a solution using Microsoft.VisualBasic.Devices. It was a pretty simple solution but with some limitation. You can only read the status of the keys, no method to set the status of the keys. (I am not going to discuss this here, I might write a separate post for it). The application deadline was very close and I was not able to implement this extra feature as I had planned. Since this feature was not requested by the client, it was delivered with read only options.

Objective

The objective of the post is to get and set the CAPS lock and NUM lock, etc. from the application.

Background

WIN32 API provides a lot of low level functions to access the system hardware. Due to high level of security, these features are not directly available in .NET Framework. To perform low level interaction with hardware, we have to call the WIN32 API methods in .NET using some special syntax.

For this purpose, System.Runtime.InteropServices namespace must be included.

Following the C language style, we have to declare the function so that we could use the method in the code.

This function is useful to simulate Key presses to the window with focus.

Parameters

bVk: Specifies a virtual-key code. The code must be a value in the range 1 to 254. For a complete list, see Virtual-Key Codes.

bScan: Specifies a hardware scan code for the key.

dwFlags: Specifies various aspects of function operation. Normally KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP are used. If specified, the key is being released. If not specified, the key is being depressed.

dwExtraInfo: Specifies an additional value associated with the key stroke.

Note: In the test application, instead of the Virtual key Codes, we will be using System.Keys enumeration where required.

Creating a Test Application

Create a new Windows Application Project in C#. From Menus & Toolbar section of the control box, add a Status Strip on your form. Add three labels to the status strip. Name them as lblINS, lblNUM and lblCAPS respectively. It is better to set their width to 40.

Set the DoubleClickEnabled property of these labels to true.

Set the AutoSize property of these labels to false.

Generate the DoubleClick event for these labels to handle the double click action, e.g.

Form Events

Set the KeyPreview property of the form to true. This will help to pass the keys pressed on the child controls to the parent control (form) (for better understanding of the property, build the attached test application with this property set to false).

In the design view, double click the form to generate its Form_Load event and call the method UpdateKeys. It will read the current status of the keys and update the form accordingly.

Similarly, the other methods for CAPS lock and INSERT can also be implemented.

Conclusion

Your application will be able to reflect the status of the toggle keys. Also you can update the status of these keys from your application by double clicking the labels in the status strip.

Although I have been reading the articles and blogs for a long time, now I decided to make my contribution by sharing my knowledge and experience. I would love to get your feedback on this and do not forget to rate the post if you like it.

Share

About the Author

I have done my BS in Computer Engineering from University of Engineering & Technology Lahore, Pakistan. Currently I am working as System Programmer and Administrator. I have worked in C, C++, C#, Assembly, Java, HTML and ASP.NET (Web Forms and MVC).
I Love to Create Visual Programs.

In my free time (if luckily, I got some,) I play Badminton and read books.

My Technical Blog:
http://techblogs.xyz

You can know about me in detail through my home page:
http://www.bilalhaider.com