有效的永久性numlock keystate检查
我购买了一个非常不错的键盘(Logitech G915),无论出于无关的原因,它都没有NUMLOCK指示器。因此,我正在使用Logitech的Lighting SDK使用密钥的RGB背光来制作功能。
我有一个非常简单的概念验证验证的概念:
while (true)
{
if (GetKeyState(VK_NUMLOCK) & 0x1)
LogiLedSetLightingForKeyWithKeyName(LogiLed::KeyName::NUM_LOCK, 0, 100, 100);
else
LogiLedSetLightingForKeyWithKeyName(LogiLed::KeyName::NUM_LOCK, 0, 0, 0);
}
但是我认为用永久性的CPU周期循环而循环以获得如此小的功能是不好的。我应该只有时间睡觉(建议长度吗?)还是有办法睡觉,直到系统发生numlock状态变化或我只是犯错了?
此外,我还没有调查过它,但是我想将其作为背景过程或托盘应用程序(没关系,只是隐藏了),所以我想答案应该牢记该限制,如果是一个。
谢谢!
I bought a really nice keyboard (logitech G915) that for whatever inane reason doesn't have a numlock indicator. Thus, I'm using Logitech's lighting SDK to make the functionality myself using the key's rgb backlight.
I have an extremely simple console proof of concept that works:
while (true)
{
if (GetKeyState(VK_NUMLOCK) & 0x1)
LogiLedSetLightingForKeyWithKeyName(LogiLed::KeyName::NUM_LOCK, 0, 100, 100);
else
LogiLedSetLightingForKeyWithKeyName(LogiLed::KeyName::NUM_LOCK, 0, 0, 0);
}
But I don't think it's good to eat up cpu cycles with a perpetual while loop for such a tiny feature. Should I just have it sleep for time (length suggested?) or is there a way to sleep until the system gets a numlock state change or am I simply going about this wrong?
Additionally, I haven't looked into it yet, but I want to make this a background process or a tray application (doesn't matter, just hidden away) so I guess answers should have that limitation in mind if it is one.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在App Startup上,使用 ) 而不是
getKeyState()
获取密钥的当前状态并相应地更新灯光。然后,使用 /code> 要安装全局挂钩每当以后按键时检测。在每个回调事件中,使用挂钩提供的状态信息,或在挂钩回调退出后立即启动异步任务以立即获取当前的密钥状态(AS
getsasynckeystate()
尚未更新调用回调),然后相应地更新灯。另外,使用 raw Input api 键盘并接收
在每个键上的窗口消息。然后获取密钥的当前状态并相应地更新灯光。wm_input
At app startup, use
GetAsyncKeyState()
instead ofGetKeyState()
to get the key's current state and update the light accordingly.Then, use
SetWindowsHookEx()
to install a globalWH_KEYBOARD_LL
hook to detect whenever the key is pressed afterwards. On each callback event, use the state information provided by the hook, or start an asynchronous task to get the current key state immediately after your hook callback exits (asGetAsyncKeyState()
has not been updated yet when the callback is called), and then update the light accordingly.Alternatively, use the Raw Input API to monitor the keyboard and receive
WM_INPUT
window messages on each key press. And then get the key's current state and update the light accordingly.