44

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RV.
  • 2,782
  • 8
  • 39
  • 51

4 Answers4

61

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pablo Retyk
  • 5,690
  • 6
  • 44
  • 59
  • 1
    I might be wrong here, but wouldn't this work strictly for a Windows release only? The first option directly hooks into a .dll, while the second requires System.Windows.Forms – Kyle Baran May 16 '15 at 12:26
  • 1
    If you are not using WinForms, (consoles for exemple) you can use `(bool) Console.CapsLock`. – Alexandre Daubricourt Apr 18 '17 at 11:50
45

With Framework 2.0 and above, you can use a framework function:

Control.IsKeyLocked(Keys) Method

public static bool NumlockActive()
{
    return Control.IsKeyLocked(Keys.NumLock);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Summer-Time
  • 1,824
  • 15
  • 19
22

If anyone comes across this thread while developing in WPF, you can use the Keyboard.IsToggled method that was introduced in .NET 3.0:

var isNumLockToggled = Keyboard.IsKeyToggled(Key.NumLock);
var isCapsLockToggled = Keyboard.IsKeyToggled(Key.CapsLock);
var isScrollLockToggled = Keyboard.IsKeyToggled(Key.Scroll);

You'll have to add the following using directive to the top of your class, if it's not already there:

using System.Windows.Input;

Internally, the IsToggled() method checks to see whether or not the KeyStates.Toggled flag is set for the specified key.

[Flags]
public enum KeyStates : byte
{
    None = (byte) 0,
    Down = (byte) 1,
    Toggled = (byte) 2,
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
11

Check State

To check state of CapsLock, NumLock and ScrollLock keys you can use Control.IsKeyLocked method:

var capsLockIsOn = Control.IsKeyLocked(Keys.CapsLock);

Actively Show The State in UI in status bar

Since the lock keys can be turn on or turn off when your application doesn't have focus handling keyboard events of the form is not enough to detect changes on the key lock state and you should also put your logic in some other places like activation event of your form or you need to register a global keyboard hook.

As a simple and reliable solution you can check their status in Application.Idle event. You must detach your idle event handler when your form closed.

public Form1()
{
    InitializeComponent();
    Application.Idle += Application_Idle;
}

void Application_Idle(object sender, EventArgs e)
{
    if (Control.IsKeyLocked(Keys.CapsLock))
        toolStripStatusLabel1.Text = "CapsLock is On";
    else
        toolStripStatusLabel1.Text = "";
}

protected override void OnFormClosed(FormClosedEventArgs e)
{
    Application.Idle -= Application_Idle;
    base.OnFormClosed(e);
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • what about if your application is not running? for eg you open a login window and you want to warning the user that caps is on – Leandro Bardelli Jul 26 '17 at 21:17
  • 1
    @LeandroTupone When your login window is showing, it means your application is running. To just check the state of key whenever you need, you can use the first option in the answer. Also to actively get aware of the state, you can use the second option. – Reza Aghaei Jul 27 '17 at 01:07