How can I find the state of NumLock, CapsLock and ScrollLock keys in .NET?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
With Framework 2.0 and above you can use an framework function
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.iskeylocked%28v=vs.80%29.aspx
Import the WinAPI function GetKeyState
and then you can use it like that
EDIT: the above is for framework 1.1, for framework 2.0 + you can use
Control.IsKeyLocked
If anyone comes across this thread while developing in WPF, you can use the Keyboard.IsToggled method that was introduced in .NET 3.0:
You'll have to add the following
using
directive to the top of your class, if it's not already there:Internally, the IsToggled() method checks to see whether or not the
KeyStates.Toggled
flag is set for the specified key.Check State
To check state of CapsLock, NumLock and ScrollLock keys you can use
Control.IsKeyLocked
method: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.