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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to know if CAPSLOCK key is Up, Down

Thread ID:

Created:

Updated:

Platform:

Replies:

172 Apr 11,2002 11:51 PM UTC Apr 13,2002 12:34 AM UTC WinForms 3
loading
Tags: FAQ
wcchan
Asked On April 11, 2002 11:51 PM UTC

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

Administrator [Syncfusion]
Replied On April 12, 2002 02:44 PM UTC

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

Clay Burch [Syncfusion]
Replied On April 12, 2002 03:05 PM UTC

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

Administrator [Syncfusion]
Replied On April 13, 2002 12:34 AM UTC

> 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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;