Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

capslock

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

like image 949
RV. Avatar asked Feb 23 '09 11:02

RV.


People also ask

How do I show caps lock and Num Lock on screen?

Click on Screen Configurations tab. In Properties window, make sure Enable on-screen display is checked. Under "Indicator settings for NumLock and CapsLock" section, look for "While the numeric lock or caps lock is ON" section, choose the "Show the indicator for a few seconds" option.

How do you know that caps lock key is on or off answer?

How to tell whether Caps Lock is on. If you pressed the Caps Lock key, you can turn it off by pressing the key again. Many keyboards have a built-in status indicator that lights up when the Caps Lock key is on. This LED is located either directly on the key or on the status bar of the keyboard, if there is one.

How do I know Num Lock is on?

To turn on the Num Lock function, press the Num Lock key until you see the LED (light) above it turn on. Likewise, to turn it off, press the Num Lock key until the LED goes out. Once the Num Lock key is pressed, it stays on until it is pressed again.

What is Num Lock state?

Num Lock or Numeric Lock (⇭) is a key on the numeric keypad of most computer keyboards. It is a lock key, like Caps Lock and Scroll Lock. Its state affects the function of the numeric keypad commonly located to the right of the main keyboard and is commonly displayed by a LED built into the keyboard.


1 Answers

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 this:

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

It is for framework 1.1. For framework 2.0 (and later) you can use:

Control.IsKeyLocked

like image 130
Pablo Retyk Avatar answered Sep 23 '22 03:09

Pablo Retyk