C# 如何在 .NET 中找到 NumLock、CapsLock 和 ScrollLock 的状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/577411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find the state of NumLock, CapsLock and ScrollLock in .NET?
提问by RV.
How can I find the state of NumLock, CapsLock and ScrollLock keys in .NET?
如何在 .NET 中找到 NumLock、CapsLock 和 ScrollLock 键的状态?
采纳答案by Pablo Retyk
Import the WinAPI function GetKeyState
导入 WinAPI 函数 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 that
然后你可以这样使用它
bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;
EDIT: the above is for framework 1.1, for framework 2.0 + you can use
编辑:以上是针对框架 1.1,对于框架 2.0 + 你可以使用
回答by Summer-Time
With Framework 2.0 and above you can use an framework function
使用 Framework 2.0 及更高版本,您可以使用框架功能
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.iskeylocked%28v=vs.80%29.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.iskeylocked%28v=vs.80%29.aspx
public static bool NumlockActive()
{
return Control.IsKeyLocked(Keys.NumLock);
}
回答by vivat pisces
If anyone comes across this thread while developing in WPF, you can use the Keyboard.IsToggledmethod that was introduced in .NET 3.0:
如果有人在 WPF 中开发时遇到此线程,您可以使用.NET 3.0 中引入的Keyboard.IsToggled方法:
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
指令添加到您的类的顶部,如果它还没有的话:
using System.Windows.Input;
Internally, the IsToggled()method checks to see whether or not the KeyStates.Toggled
flag is set for the specified key.
在内部,IsToggled()方法检查是否KeyStates.Toggled
为指定的键设置了标志。
[Flags]
public enum KeyStates : byte
{
None = (byte) 0,
Down = (byte) 1,
Toggled = (byte) 2,
}
回答by Reza Aghaei
Check State
检查状态
To check state of CapsLock, NumLockand ScrollLockkeys you can use
Control.IsKeyLocked
method:
要检查CapsLock,NumLock和ScrollLock键的状态,您可以使用
Control.IsKeyLocked
方法:
var capsLockIsOn = Control.IsKeyLocked(Keys.CapsLock);
Actively Show The State in UI in status bar
在状态栏中的 UI 中主动显示状态
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.
作为一个简单可靠的解决方案,您可以在Application.Idle
事件中检查它们的状态。当您的表单关闭时,您必须分离您的空闲事件处理程序。
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);
}