Reputation: 13
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!
Upvotes: 1
Views: 119
Reputation: 596833
At app startup, use GetAsyncKeyState()
instead of GetKeyState()
to get the key's current state and update the light accordingly.
Then, use SetWindowsHookEx()
to install a global WH_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 (as GetAsyncKeyState()
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.
Upvotes: 1