Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

How to retrieve the state of Caps Lock, Num Lock and Scroll Lock keys

On Sunday, September 23rd 2007 at 02:27 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.5 with 2 votes)
Advertisement
More C# Resources
In order to know whether or not Caps Lock, Num Lock or Scroll Lock keys are on, we need to make use of the Win32 API through a call to an unmanaged function.

Since we'll make a call to an unmanaged function, the following using statement is in order:

using System.Runtime.InteropServices;


The following is the definition for the unmanaged function we'll be using, GetKeyState():

// An umanaged function that retrieves the states of each key

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]

public static extern short GetKeyState(int keyCode);


And finally we retrieve the state of each of the three keys and show it in a message box:

// Get they key state and store it as bool

bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;

bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;

bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;

 

// Show the status

MessageBox.Show("Caps Lock is on: " + CapsLock.ToString());

MessageBox.Show("Num Lock is on: " + NumLock.ToString());

MessageBox.Show("Scroll Lock is on: " + ScrollLock.ToString());


Here is the complete source code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace Labs

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        // An umanaged function that retrieves the states of each key

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]

        public static extern short GetKeyState(int keyCode);

 

        private void Form1_Load(object sender, EventArgs e)

        {

            // Get they key state and store it as bool

            bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;

            bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;

            bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;

 

            // Show the status

            MessageBox.Show("Caps Lock is on: " + CapsLock.ToString());

            MessageBox.Show("Num Lock is on: " + NumLock.ToString());

            MessageBox.Show("Scroll Lock is on: " + ScrollLock.ToString());

        }

    }

}

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by The_Assimilator on Thursday, January 24th 2008 at 05:02 PM

If you're using the .NET Framework 2.0 or later, you can use the built-in Control.IsKeyLocked() method instead:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.iskeylocked(VS.80).aspx

by Sywesk on Friday, April 2nd 2010 at 03:02 PM

Thanks for this great post. It's very useful for who develop with the XNA Framework.

by Az on Friday, September 17th 2010 at 03:33 AM

Better and platform independent way is now avaiable:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.iskeylocked.aspx

by Asif on Monday, October 11th 2010 at 02:32 AM

Very helpful
Thank you GeekPedia

by Dương Tấn Thành on Friday, November 5th 2010 at 12:35 PM

Here is what I need!
Thank you very much!

by pcabanel on Monday, May 2nd 2011 at 06:48 AM

Thank you very much!

by pcabanel on Monday, May 2nd 2011 at 06:48 AM

Thank you very much!

by Harini on Tuesday, May 24th 2011 at 12:23 AM

I used this code but i got error in this line
bool CapsLock = (((ushort)GetKeyState(0x14))

by best balance transfer credit cards on Tuesday, December 20th 2011 at 08:12 PM

It was very useful for me, thank you very much!

by Alfred on Wednesday, August 1st 2012 at 01:51 AM

I used the method and got the Exception.
MethodAccessException

by Jean-Michel on Thursday, November 29th 2012 at 11:30 AM

It is even easier than what was said here:

' NumLock handling:
If System.Windows.Input.Keyboard.IsKeyToggled(Key.NumLock) Then
NumLockState = "NUM"
Else
NumLockState = ""
End If

by Md.Jahurul Islam on Wednesday, August 28th 2013 at 11:19 AM

Nice Code.


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources