tags:

views:

495

answers:

3

How can I find the state of NumLock, CapsLock and ScrollLock keys in .net ?

+3  A: 

Import the WinAPI function GetKeyState

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);

and then you can use it like that

bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;

EDIT: the above is for framework 1.1, for framework 2.0 + you can use

Control.IsKeyLocked

pablito
+1  A: 

You can find the answer here:

http://www.geekpedia.com/KB122_How-to-retrieve-the-state-of-Caps-Lock-Num-Lock-and-Scroll-Lock-keys.html

Which was found with the google terms "caps lock status c#"

MrWiggles
+1  A: 

You'll need to use the Win32 API for this.

Take a look here:

http://www.geekpedia.com/KB122_How-to-retrieve-the-state-of-Caps-Lock-Num-Lock-and-Scroll-Lock-keys.html

MrWiggles beat me to it

Bart S.