Reputation: 312
I've got a program written in C++ and running on windows. I allow the user to keybind the CAPSLOCK key so it would be nice if, every time they pressed it, they weren't also toggling their CAPSLOCK state.
I've found no way to "capture" the CAPSLOCK message in order to prevent it from being registered by windows. It seems like by the time I get a WM_KEYDOWN message, the indicator light on my keyboard has already switched.
I did find this suggested code:
keybd_event( VK_CAPITAL, 0x3a, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( VK_CAPITAL, 0x3a, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
But, it doesn't work. It kinda caused the indicator light to flicker, and doesn't seem to affect the CAPSLOCK state at all.
Upvotes: 4
Views: 1787
Reputation: 511
While the fake keyboard events via keybd_event work to toggle the state, if you really want to block capslock you'll need to do a keyboard filter hook, ala
HHOOK hhkLowLevelKybd = SetWindowsHookEx( WH_KEYBOARD_LL,
LowLevelKeyboardProc,
NULL,
0 );
and then in your hook you look for and don't pass on the key(s) you want blocked, like this (fragment adapted from working code, but untested):
LRESULT CALLBACK LowLevelKeyboardProc( int nCode,
WPARAM wParam,
LPARAM lParam)
{
int fEatKeystroke = 0;
if (nCode == HC_ACTION)
{
// Get hook struct
PKBDLLHOOKSTRUCT p = ( PKBDLLHOOKSTRUCT ) lParam;
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
if (p->vkCode == VK_CAPITAL)
fEatKeystroke =1;
}
}
// Did we trap a key??
return( fEatKeystroke ? 1 : CallNextHookEx( NULL, nCode, wParam, lParam ));
}// End LowLevelKeyboardProc
Upvotes: 0
Reputation: 172
Try it in reverse order:
keybd_event( VK_CAPITAL, 0x3a, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
keybd_event( VK_CAPITAL, 0x3a, KEYEVENTF_EXTENDEDKEY, 0 );
Upvotes: 2