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

后端 未结 4 1312
忘掉有多难
忘掉有多难 2020-11-27 18:41

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

4条回答
  •  醉酒成梦
    2020-11-27 19:01

    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,
    }
    

提交回复
热议问题