I want to put inside a software the necessary codes for it to disable Windows (Xp, Vista, specially 7 and sucessors) hotkeys that could allow the user to get away from the software.
Details:
I did look at a post here in stackoverflow that had a very similiar case as mine (Prevent users from quitting a windows application via system hotkeys), but as far as I understood nether of the solutions presented were applicable to my specific situation, and I didn't find anything in the web as well.
Ok, I got how to do it. The code is able to create a sistem-wide hook without DLL using a low level keyboard hook. Here is the code (better that explaining)(using Qt):
//Installing the hook
SWH_return = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,GetModuleHandle(NULL),0);
if (SWH_return != NULL)
qDebug() << "Hook true";
else
qDebug() << "Hook false";
//Uninstalling the hook
bool teste = false;
teste = UnhookWindowsHookEx(SWH_return);
if (teste)
qDebug() << "Unhook: true";
else
qDebug() << "Unhook: false";
//The function responsible for detecting the keystrokes
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
return CallNextHookEx(NULL, nCode, wParam, lParam);
tagKBDLLHOOKSTRUCT *str = (tagKBDLLHOOKSTRUCT *)lParam;
switch(str->flags)
{
case (LLKHF_ALTDOWN):
qDebug() << "ALT";
delete str;
return 1;
}
if (wParam == WM_KEYDOWN)
{
switch (str->vkCode)
{
case VK_RWIN:
case VK_LWIN:
case VK_LCONTROL:
case VK_RCONTROL:
case VK_APPS:
case VK_SLEEP:
case VK_MENU:
qDebug() << "SPECIAL PRESS";
delete str;
return 1;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
This last function don't need any declaration in the .h or in the .cpp file. It blocks inputs of Ctrl, Windows Key and Alt. The other two must be placed respectively in the functions where the user wants to begin the key disabling and when he want it to stop.
Thanks,
Momergil.