C#主动检测锁定键 [英] C# Actively detect Lock keys
问题描述
我有一个无线键盘和鼠标,没有任何锁定指示器,也没有捆绑提供视觉帮助的任何软件,所以我要自己做.
I have a wireless keyboard and mouse that doesn't have any lock indicators, nor any software bundled to provide a visual aid, so I'm making my own.
我知道了,所以如果我单击notifyIcon,它将列出打开了哪些锁,但是我想通过在锁打开后提醒我来使其变得更聪明.
I got it so that if I click on a notifyIcon it'll list which lock(s) are turned on, but I'd like to make it smarter by alerting me once the locks are engaged.
我在网上找到了一些物品,但是说实话,我只想要锁定键,我不在乎其他键盘按键.
I found a few items online, but honestly I just want the lock keys, I don't care about any other keyboard presses.
我正在使用C#.NET 4,但如果该版本有功能,我可以使用.NET 4.5.
I'm using C# .NET 4, though I can use .NET 4.5 if there's something with that version.
谢谢.
推荐答案
您将需要注册某种键盘挂钩来监听按键,然后像这样检索锁定键的状态:
http://blogs.msdn.com/b/toub /archive/2006/05/03/589423.aspx
You will want to register some sort of keyboard hook to listen for the key presses and then retrieve the state of the lock keys like this:
http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx
除了上面的文章外,还进行了以下修改以捕获锁定键的状态:
In addition to the above article, make the below modifications to capture state of the lock keys:
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
Keys key = (Keys)vkCode;
if (key == Keys.Capital)
{
Console.WriteLine("Caps Lock: " + !Control.IsKeyLocked(Keys.CapsLock));
}
if (key == Keys.NumLock)
{
Console.WriteLine("NumLock: " + !Control.IsKeyLocked(Keys.NumLock));
}
if (key == Keys.Scroll)
{
Console.WriteLine("Scroll Lock: " + !Control.IsKeyLocked(Keys.Scroll));
}
Console.WriteLine((Keys)vkCode);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
这篇关于C#主动检测锁定键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!