We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to know if CAPSLOCK key is Up, Down

Under Platform SDK, we can use GetKeyState. What's under .Net Framework. Brgds

3 Replies

AD Administrator Syncfusion Team April 12, 2002 10:44 AM

I don't see .Net providing a simple way to do this. So, you have to use the good old GetKeyState directly. Declare this class first: [ ComVisibleAttribute(false), SuppressUnmanagedCodeSecurityAttribute() ] internal class NativeMethods { [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)] public static extern short GetKeyState(int keyCode); public static int HIWORD(int n) { return ((n >> 16) & 0xffff/*=~0x0000*/); } public static int LOWORD(int n) { return (n & 0xffff/*=~0x0000*/); } } Then when you want to check if Caps is down, call: short state = NativeMethods.GetKeyState(0x14 /*VK_CAPTIAL*/); bool capsKeyDown = NativeMethods.HIWORD(state); -Praveen > Under Platform SDK, we can use GetKeyState. What's under .Net Framework. > > Brgds


CB Clay Burch Syncfusion Team April 12, 2002 11:05 AM

There is a method for getting modifier key states on the keyboard. It is discussed in our FAQ entry: http://www.syncfusion.com/faq/winforms/search/656.asp

BUT this technique does not work for the CAPS LOCK key. So, the only way I know to get the state of the CAPS LOCK key is to use P/Invoke. Below is some code that seem to work for me.

using System.Runtime.InteropServices;
...
...
[DllImport("user32.dll")] 
public static extern long GetKeyState(int vKey); 

const int VK_CAPITAL = 0x14;

private void button1_Click(object sender, System.EventArgs e)
{
	if( GetKeyState(VK_CAPITAL) != 0)
		Console.WriteLine("the X-CapLock key is down");
}
HTH, Clay Burch


AD Administrator Syncfusion Team April 12, 2002 08:34 PM

> I don't see .Net providing a simple way to do this. > > So, you have to use the good old GetKeyState directly. > > Declare this class first: > [ > ComVisibleAttribute(false), > SuppressUnmanagedCodeSecurityAttribute() > ] > internal class NativeMethods > { > [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)] > public static extern short GetKeyState(int keyCode); > > public static int HIWORD(int n) > { > return ((n >> 16) & 0xffff/*=~0x0000*/); > } > > public static int LOWORD(int n) > { > return (n & 0xffff/*=~0x0000*/); > } > } > > Then when you want to check if Caps is down, call: > > short state = NativeMethods.GetKeyState(0x14 /*VK_CAPTIAL*/); > > bool capsKeyDown = NativeMethods.HIWORD(state); > > -Praveen > > > > Under Platform SDK, we can use GetKeyState. What's under .Net Framework. > > > > Brgds

Loader.
Live Chat Icon For mobile
Up arrow icon