How can I find the state of NumLock, CapsLock and ScrollLock keys in .NET?
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,
}