I have set up a low level keyboard hook to filter out any unwanted keys that would allow a user (in this case my two year old daughter) from switching to another app or otherwise being able to leave the current app. The only problems so far are the sleep (VK_SLEEP) and Win + 'L' (Log Off) keys. These both seem to have their default actions taken even before my hook function is called.
Does anyone have any idea how to disable these two keys while my app is running?
DWORD BadVKCodes[] = { VK_MENU, VK_PAUSE, VK_PRINT, VK_EXECUTE, VK_SNAPSHOT,
VK_HELP, VK_LWIN, VK_RWIN, VK_APPS, VK_SLEEP, VK_F1,
VK_BROWSER_SEARCH, VK_BROWSER_FAVORITES, VK_BROWSER_HOME,
VK_VOLUME_MUTE, VK_VOLUME_DOWN, VK_VOLUME_UP,
VK_MEDIA_NEXT_TRACK, VK_MEDIA_PREV_TRACK, VK_MEDIA_STOP,
VK_LAUNCH_MAIL, VK_LAUNCH_MEDIA_SELECT, VK_LAUNCH_APP1, VK_LAUNCH_APP2,
VK_ICO_HELP, VK_PROCESSKEY, VK_PACKET };
LRESULT CALLBACK LowLevelKeyboardHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0 || g_hWnd == NULL)
{
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
KBDLLHOOKSTRUCT *khs = reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
const int VKCodesCount = _countof(BadVKCodes);
if ((khs->flags & LLKHF_ALTDOWN) == LLKHF_ALTDOWN)
{
return 1;
}
if (std::find(BadVKCodes, BadVKCodes + VKCodesCount, khs->vkCode) != BadVKCodes + VKCodesCount)
{
return 1;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}